2

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!

3

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.

3
  • 1
    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. – Andrew Lambert 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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