Move your website properly using htaccess

Sometimes you need to move an entire website to a new domain name. There’s lots of ways to do this, but if you’re not careful you might end up losing or confusing visitors (and google). Here’s a quick tutorial to help you out…

Of course you could put a notice on your old site linking through to the new page on the new site, but that would be clunky. Also you could do a javascript redirect, but that’s not much better. Ideally you want to send visitors through to the new site so they reach the content they want before they realise they’ve gone to another domain. For this (assuming you’re using apache), my first choice would be to use the htaccess file.

Single Page Redirects

Redirect 301 /page.html http://www.example.com/page.html

The line above in a .htaccess file in the root of your old website will send people visiting a specific page, through to a specific page on the new site. If you’ve got lots of pages though, it’s going to be a real pain to list them all.

Entire Site Redirect

Redirect 301 / http://www.example.com/

This will redirect any traffic to your old site to the root of the new site. However, with htaccess we can do better than that…

Entire Site Redirect (Preserving Directories & Pages)

If you have mod rewrite setup in Apache, the following lines in your htaccess file will make sure people get taken straight through to the correct pages on the new site.

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

The last example will make sure that your visitors and search engines can find your new content quickly and easily. Also you may notice the ‘301‘ listed in all of these examples – this lets google and other search engines know that the move is permanent.

Leave a Reply

Your email address will not be published. Required fields are marked *