.htaccess 301 redirect, same server
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!
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.