bigben
bigben

Reputation: 3381

htaccess redirect subdomain

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?

Upvotes: 4

Views: 21427

Answers (2)

Aslam Khan
Aslam Khan

Reputation: 378

You need to run your index.php file

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]

Upvotes: 0

Ray
Ray

Reputation: 41508

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>

Upvotes: 3

Related Questions