Forum Moderators: phranque

Message Too Old, No Replies

Need help with 301 redirect code!

         

ichthyous

3:25 pm on Jan 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi there, I have two subdomains of my main site that I would like to redirect, but for some reason the redirect code isn't working. The subdomains are hosted out of a separate folders in the www area of the server, and each folder has it's own index.html file. I tried placing an htaccess file in each folder separately, but it didn't work so I figure I have to place the code in the main htaccess file for the domain, not for the subdomains. Here is what I have:

RewriteEngine On
RewriteCond %{HTTP_HOST} "!^subdomain\.mysite\.com"
RewriteRule (.*) [mysite.com...] [R=301,L]

Caterham

4:31 pm on Jan 25, 2005 (gmt 0)

10+ Year Member



Hi,

you should remove the double quotes from the condition pattern.

and [...] would not work, because this is an invalid URL.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^subdomain\.mysite\.com
RewriteRule ^(.*) http://www.mysite.com/$1 [R=301,L]

If you cannot access your subdomain from your root domain (e.g. subdomain points to subfolders of the domain or is completely out of the space of the root domain), you have to place the .htaccess into the document root of the subdomain.

Remember that the [R]-Flag forces an external redirect, the displayed URL in the browser's location bar would change.

explicitly forced redirect!= internal redirect

If you'd like to keep the URL, you've to use mod_rewrite in combination with mod_proxy (proxy-pass). Simply change the flag from

[R=301,L]

to
[P,L]

This would cause double traffic, once of subdomain.mysite.com (to the user) and mysite.com (to the server/proxy-module).

Rob

ichthyous

4:52 pm on Jan 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I made a mistake in cut and pasting the URL...there is only one "http"...I will try what you say to the best of my ability...it's a bit advanced for me so Im not sure i understand everything you say

jdMorgan

12:15 am on Jan 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let's translate this code:

# Turn the rewrite engine on
RewriteEngine On
# IF the requested host *IS NOT* subdomain.mysite.com
RewriteCond %{HTTP_HOST} !^subdomain\.mysite\.com
# then redirect to www.mysite.com
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]

So, this code sets up an infinite redirection loop, because, having redirected to www.mysite.com, the next request will be to www.mysite.com, and not to subdomain.mysite.com, so it will redirect again.

To fix it, you need to remove the "!" negation oprerator:


RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mysite\.com
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]

This should work as long as it is placed in a directory that the server traverses while looking up the requested resource. Because of differences in the way that subdomains are mapped to subdirectories, it may work in your Web root directory, or it may not. It should work if placed in the subdirectory for the (sub)domain that you want to redirect to the other (sub)domain.

If it appears that this code is never executed, check your server error log. It may contain an error message telling you that you need to enable SymLinks, which is required by mod_rewrite. If so, add the line


Options +FollowSymLinks just before the code shown above.

Jim