dMa
asked on
301 redirect of a subdomain in Apache
I'm redirecting a subdomain in .htaccess this way:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule ^$ http://www.domain.com/en/keyword [R=301,L]
On the first look, this works fine, but not entirely.
http://subdomain.domain.com/ gets redirected to http://www.domain.com/en/keyword
http://subdomain.domain.com/en/keyword does not get redirected at all!
I need a solution that will redirects all URLs of a specific subdomain to a single page.
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule ^$ http://www.domain.com/en/keyword [R=301,L]
On the first look, this works fine, but not entirely.
http://subdomain.domain.com/ gets redirected to http://www.domain.com/en/keyword
http://subdomain.domain.com/en/keyword does not get redirected at all!
I need a solution that will redirects all URLs of a specific subdomain to a single page.
Easiest is to make virtual host for sub-domain and add a single redirect there.
Can you turn on your RewriteLog? If so, set the level to 9 and post the log for a single browsing attempt to a URL that does not forward.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or, even shorter than mwecomputers' four config lines, you can create a subdomain virtual host in Apache, and then just include this one line:
Redirect 301 / http://server.com/subdirectory/
Redirect 301 / http://server.com/subdirectory/
In reference to mwecomputers' suggestion:
1) the [NC] flag may be important for you. Make sure it is in your rule.
2) the rule highlights what I missed: the resource detection. The difference being that you want all requests to go to a single page, so alter the rule as follows:
As far the recommendation for a separate virtual host, it will work (indeed, my preferred method) if you have access to the server's config file. Your question asked only about .htaccess, which means mod_rewrite is probably your best bet.
1) the [NC] flag may be important for you. Make sure it is in your rule.
2) the rule highlights what I missed: the resource detection. The difference being that you want all requests to go to a single page, so alter the rule as follows:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule .* http://www.domain.com/en/keyword [R=301,L]
As far the recommendation for a separate virtual host, it will work (indeed, my preferred method) if you have access to the server's config file. Your question asked only about .htaccess, which means mod_rewrite is probably your best bet.
Your RewriteRule specifically states that it should only redirect where there is NOTHING after the HOST by specifying ^$:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule ^$ http://www.domain.com/en/keyword [R=301,L]
I think what you WANTED is:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule .* http://www.domain.com/en/keyword [R=301,L]
This will effectively redirect WITH or WITHOUT a path following the HOST.
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule ^$ http://www.domain.com/en/keyword [R=301,L]
I think what you WANTED is:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule .* http://www.domain.com/en/keyword [R=301,L]
This will effectively redirect WITH or WITHOUT a path following the HOST.
ASKER
Several correct answers, but mwecomputers was first.
ASKER