Amygdala
Amygdala

Reputation: 83

redirect any subdomain to main domain with .htaccess

I'm attempting to redirect any direct attempts to access sub.example.com/login over to the original domain/uri at example.com/login, and from the number of questions I've already read in regards to this exact thing, it would appear easy on the surface of it...

I don't know if there's something going on that's overriding the desired outcome, but I have the following rules in my .htaccess file, yet I'm still able to access sub.example.com/login without being redirected.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com/login$  [NC]
RewriteRule ^(.*)$ http://example.com/login$1        [L,R=301]

Actually, just for complete clarity, here's my entire .htaccess file contents. Maybe somebody else can see something wrong that I'm missing.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

# redirect subdomain login attempts to main domain
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com/login$  [NC]
RewriteRule ^(.*)$ http://example.com/login$1        [L,R=301]

Upvotes: 1

Views: 1791

Answers (2)

danielpopa
danielpopa

Reputation: 820

RewriteEngine On
RewriteBase /

# Redirect all subdomains to example.com
RewriteCond %{HTTP_HOST} !^(.*)\.example\.com$ [NC]
# Redirect only bar & foo subdomains to example.com
#RewriteCond %{HTTP_HOST} !^(bar|foo)\.example\.com$ [NC]
RewriteRule ^ http://example.com/ [L,R]

Upvotes: 0

KNOWARTH
KNOWARTH

Reputation: 922

Put Redirect subdomain login rewrite condition & rule just after RewriteBase line. That should help to address your issue.

Upvotes: 1

Related Questions