Link to home
Start Free TrialLog in
Avatar of dMa
dMaFlag for Germany

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.



Avatar of dMa
dMa
Flag of Germany image

ASKER

Increased points to 500.
Easiest is to make virtual host for sub-domain and add a single redirect there.
Avatar of Steve Bink
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
Avatar of Michael Worsham
Michael Worsham
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nemagee
nemagee

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/
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:

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule .* http://www.domain.com/en/keyword [R=301,L]

Open in new window


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.
Avatar of dMa

ASKER

Several correct answers, but mwecomputers was first.