htaccess redirect subdomain

21,401

Your rewrite rule logic in plain speak is doing the following:

  1. For ANY HOST other thant www.mysite.co (including foo.mysite.com, blog.mysite.com, etc..)
  2. Permanent Redirect (301) to http://www.mysite.co/
  3. This is the last rule to check in the .htaccess file, so bail out

To not redirect your own subdomains, the easiest and clearest way is to handle it explicitly with more rewrite conditions (see example below). For more kung fu htaccess fun try this: .htaccess Wildcard Subdomains

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.mysite.co$ [NC]        
    RewriteCond %{HTTP_HOST} !^blog.mysite.co$ [NC]
    RewriteCond %{HTTP_HOST} !^foo.mysite.co$ [NC]
    RewriteRule ^(.*)$ http://www.mysite.co/$1 [R=301,QSA,L]

 </IfModule>
Share:
21,401
Admin
Author by

Admin

Updated on October 01, 2020

Comments

  • Admin
    Admin over 3 years

    I have the following .htaccess code:

    <IfModule mod_rewrite.c>
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.mysite.co$ [NC]
    RewriteRule ^(.*)$ http://www.mysite.co/$1 [R=301,QSA,L]
    
    </IfModule>
    

    The problem is that all subdomains get redirected to www.mysite.co/subdomain How can I aviod this?