RNK
RNK

Reputation: 5792

redirect subdirectory to subdomain issue

I have .htaccess file like:

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

It works fine in market subdirectory. It redirects to the subdomain. But, There is a problem with other 2 subdirectories.

ERROR FOR OTHER 2 sub-domains:

The page isn't redirecting properly

What should I do to overcome this problem?

Upvotes: 3

Views: 3478

Answers (1)

anubhava
anubhava

Reputation: 786289

It is because RewriteCond is only applicable to the very next RewriteRule. Your last 2 rules execute for all the hosts including buddies and bazaar and causes redirection loop.

You need these rules:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteRule ^/?market/(.*)$ http://market.example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteRule ^/?buddies/(.*)$ http://buddies.example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteRule ^/?bazaar/(.*)$ http://bazaar.example.com/$1 [L,R=301]

Upvotes: 5

Related Questions