Redirecting a subdirectory to a subdomain problem

I have a .htaccess file, for example:

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 great in a subdirectory of the market. It redirects to a subdomain. But there is a problem with the other 2 subdirectories.

ERROR FOR OTHER 2 subdomains:

The page isn't redirecting properly

What should I do to overcome this problem?

+3
source share
1 answer

This is because it RewriteCondapplies only to the very next RewriteRule. Your last 2 rules are followed for all hosts, including buddiesand bazaar, and cause a redirect cycle.

You need the following 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]
+5
source

All Articles