.htaccess isn’t redirecting subdomain

I have setup an .htccess file to redirect www to non-www and also remove trailing slashes. I also have to make sure that business.domain.com also redirects (301) to domain.com. The other rules work except redirecting business.domain.com.

<IfModule mod_rewrite.c>
RewriteEngine On

# redirect to non-www
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# redirect business subdomain to no subdomain
RewriteCond %{HTTP_HOST} ^business.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# redirect non-trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Any ideas why the 2nd cond/rule isn’t taking effect?

Related posts

Leave a Reply

2 comments

  1. You might be running into a trailing slash. How about either:

    # redirect business subdomain to no subdomain
    RewriteCond %{HTTP_HOST} ^business.(.+)/?$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
    

    Or just

    # redirect business subdomain to no subdomain
    RewriteCond %{HTTP_HOST} ^business. [NC]
    RewriteRule ^ http://domain.com%{REQUEST_URI} [NE,R=301,L]
    
  2. For your information, you’re making a mistake when you redirect to a top level domain (unless you have huge bandwidth): setting cookies for a prefixless domain sets them across all subdomains… and sessions always set cookies.

    Anyway, here’s a rule that works flawlessly for me:

    # If only two groups separated by a '.':
    RewriteCond %{HTTP_HOST} ^mywebsite.(fr|com|net|org|eu) [NC]
    # This means there's no www => force redirection:
    RewriteRule (.*) http://www.mywebsite.%1$1 [QSA,R=301,L]
    

    And I have the opposite as well:

    # "business" => no subdomain:
    RewriteCond %{HTTP_HOST} ^(business.)mywebsite.(fr|com|net|org|eu) [NC]
    RewriteRule (.*) http://mywebsite.%2$1 [QSA,R=301,L]