PDA

View Full Version : 301 redirect for a subdomain


gennsearch
10-05-2007, 12:42 PM
a site that i look after has 4 subdomains but the subdomains are
www.subdomain.domain.com
the other sites that I look after all the subdomains are all non www.

this is what I have in my htaccess file for the site that redirects the non subdomains properly

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.subdomain\.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.subdomain1\.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.subdomain\2.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

what can I add so that the www subdomains redirect properly.

Thanks in advanced.


Robert

Jazajay
10-16-2007, 10:35 PM
Hi buddy you have 2 syntax errors.

1.You need to add a OR as in - if it's not... OR this... OR this... redirct it to this....

To do that add this [NC,OR] instead of [NC].

The second syntax error is in this line
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
it needs to be this
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Just as a side point I'm not 100% sure you need
RewriteCond %{HTTP_HOST} .
Take it out if it doesn't work add it but it doesn't look like you will need it.

It should then redirct properly if you are still having issues let me know.

Jaza

gennsearch
10-17-2007, 11:04 AM
this is what I've been using and works great, let me know if you think the syntax needs to be changed


Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.subdomain.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]



Thanks Robert

Jazajay
10-17-2007, 11:19 AM
Looks good the only slight changes I would make is to put a \ in front of the dots in the condition as this escapes those characters and you also don't need $ at the end of the first or second rewrite condition as this character matches the end of the string in the RewriteRule and is not needed in the condition statement and only need in the 5th line where you have it.

So I would change it to-

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.subdomain\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Apart from that it looks good.

Obviously test it on a page that isn't live first if at all possible.

Jaza