Let's get that site 301 redirect done.

There are many considerations when moving site address, changing hosts, migrating CMS's etc.
For that reason, here's a list of what I consider to be the most commonly used bits of codes & tips for getting a site 301 / redirect carried out smoothly.
The basic steps before you do anything:
  1. Get an idea of all your URL's, generate a site map, and ensure all the current URL's will either be existing on the new site or 404 errors will be handled properly.
  2. Ensure all resources (images/files) are on the new domain/host.
  3. Make sure both the current site (the one you're about to 301) and the new site are verified with Google webmasters.
  4. Put the site 301 code in place.
  5. Tell Google (and others) about the change of address (see here).

301 Options using the .htaccess file (root of your server)

.htaccess - Basic 301 for entire site

This is the most basic 301 redirect for a website to a new domain/address. Put this in your .htaccess file and all URL's will be kept intact. eg: http://OldSiteAddress.com/my-example-page/ will 301 to http://NewSiteAddress.com/my-example-page/


RewriteEngine on
Redirect 301 / http://NewSiteAddress.com/

        

Slighty stricter version of the basic 301

The standard 301 htaccess above can on the rare occasion affect other sites if you're on a shared hosting package with sites inside sites. For that reason this solution is stricter, and only applies if the current site url condition is met.


RewriteEngine on
RewriteCond %{HTTP_HOST} ^OldSiteAddress\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.OldSiteAddress\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.NewSiteAddress\.com\/$1" [R=301,L]

        

Rewrite WWW to non WWW

This will remove the WWW extension from your site, making it a cleaner URL.


RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

        

Force WWW extension

If for some reason you want to show the WWW before your website URI, this .htaccess code will just do that - forcing the WWW extension for all non sub-domain requests.


RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

        

301 for Only 1 Directory

Sometimes you will only want to 301 redirect one directory of your site (maybe /news/ for example). This 301 redirect only matches and applies if it matches the directory address you supply.


RewriteEngine on
RedirectMatch 301 /directoryname/(.*) http://NewSiteAddress.com/directoryname/$1

        

Rewrite all pages to 1 single page/website

This will redirect every single request to a single page, useful for a 1 page website, or if you're 301'ing a whole site and not maintaining any URL's, so pushing them all to a homepage.


RewriteEngine on
RedirectMatch 301 ^(.*)$ http://www.NewSiteAddress.com

        

Rewrite all HTTP requests to HTTPS

Have you switched/upgraded to SSL/HTTPS? This simple .htaccess code ensures all requests that are made to HTTP get redirected to HTTPS. It also enforces WWW, if you're already handling that, then just remove the last 2 lines.


RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Enforce www:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

        

Moving just 1 directory to another site, but forcing a new extension type.

Possibly you're 301'ing a section of an old site that uses a mix of php and html extensions and want to convert them now all to php extensions, this .htaccess code will do just that!


RewriteEngine on
RedirectMatch 301 /directory/(.*)\.(php|html) http://www.NewSiteAddress.com/directory/$1.php

        

301 using PHP from an index.php file.

If your site uses a common index file to handle all your url's and site content you can simply put this at the top of it to redirect all traffic to the new address.


<?php
header("Location: http://www.NewSiteAddress.com/", true, 301);
exit();
?>