How to set permanent 301 redirect with htaccess?

How do I 301 Permanent Redirect with .htaccess when the TLD is different?

  • When redirecting an entire website using a .htaccess file, what is the proper code the the TLD (top level domain) is different? I need to make sure both the domain and all the subpages (which have the same names/url structure) redirect seamlessly. For example: http://www.olddomain.com to http://www.newdomain.net I found the following code online: Options +FollowSymLinks RewriteEngine on # RewriteCond %{HTTP_HOST} !^www\.newdomain\.com RewriteRule (.*) http://www.newdomain.com/%241 [R=301,L] but just need to know how to tweak it so that the .com and .net changes as well. I'm assuming that I would just change the .com to .net in both the condition and the rule, but I need a little confidence. Also, does the above code take care of the canonical redirect to add the www. when someone searches without it?

  • Answer:

    Yes, just tweak the domains in both lines: RewriteCond %{HTTP_HOST} !^www\.newdomain\.net RewriteRule (.*) http://www.newdomain.net/%241 [R=301,L] To answer your second question yes this also handles people visiting your site without the www. HTTP_HOST is the hostname from the request. So in plain English those lines mean "if HTTP_HOST is not http://www.newdomain.net, redirect the user to http://www.newdomain.net/$1. $1 (in ths example) is the request path, so this is appended on to the end of the domain. And the R=301 tells it to send a 301 (Moved Permanently) header.

Tim Fountain at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.