I set up a website at mywebsite.net, but recently bought the domain mywebsite.com.

Now I updated the information on Google via Webmaster Tools and it recommended we set up a 301 redirect from the .net to the .com. I have found how to do this by adding code like this to the .htaccess:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

My problem is, both domains point to the same server, so am I correct in assuming this would try to redirect even if I visited the .com initially? If so, what is the correct way to accomplish this?

It may not matter, but I didn't want to cause any problems with my search rankings.

Thanks in advance!

link|improve this question

60% accept rate
feedback

1 Answer

up vote 1 down vote accepted

For this use case I'd add a rewrite condition (RewriteCond) to be explicit about which domain you want to impose the rule against. E.g.:

Options +FollowSymLinks
RewriteEngine on  
RewriteCond %{HTTP_HOST} ^www.newdomain.net [NC]  
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

This way if the condition isn't met, your redirect won't fire. Also note that these can be chained, so you can assign multiple conditions.

See http://httpd.apache.org/docs/current/mod/mod_rewrite.html#Solutions for more info. Very handy stuff lurking in that page.

link|improve this answer
Thanks, that worked fine :) It doesn't work for subdirectories however, for example newdomain.net/site/ does not redirect to newdomain.com/site/ - do you know how I could accomplish this? – user491704 Dec 19 '11 at 16:20
I think if you remove the $ symbol from the third line it will work for sub-directories. – Amazed Dec 19 '11 at 17:53
Thanks to you both - I've edited the original post. The "$" does anchor the condition to a 'bare' hostname (specifically, the end of line), so removing it should work for you. – mcauth Dec 19 '11 at 18:28
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.