Thank you Moz - https://moz.com/learn/seo/redirection
Redirection is the process of forwarding one URL to a different URL. There are three main kinds of redirects: 301, 302, and meta refresh.
A redirect is a way to send both users and search engines to a different URL from the one they originally requested. Below are descriptions of some of the commonly used types of redirects.
A 301 redirect is a permanent redirect which passes between 90-99% of link juice (ranking power) to the redirected page. 301 refers to the HTTP status code for this type of redirect. In most instances, the 301 redirect is the best method for implementing redirects on a website.
A 302 redirect is a temporary redirect. It passes 0% of link juice (ranking power) and, in most cases, should not be used. The Internet runs on a protocol called HyperText Transfer Protocol (HTTP) which dictates how URLs work. It has two major versions, 1.0 and 1.1. In the first version, 302 referred to the status code "Moved Temporarily." This was changed in version 1.1 to mean "Found."
A 307 redirect is the HTTP 1.1 successor of the 302 redirect. While the major crawlers will treat it like a 302 in some cases, it is best to use a 301 for almost all cases. The exception to this is when content is really moved only temporarily (such as during maintenance) AND the server has already been identified by the search engines as 1.1 compatible. Since it's essentially impossible to determine whether or not the search engines have identified a page as compatible, it is generally best to use a 302 redirect for content that has been temporarily moved.
Meta refreshes are a type of redirect executed on the page level rather than the server level. They are usually slower, and not a recommended SEO technique. They are most commonly associated with a five-second countdown with the text "If you are not redirected in five seconds, click here." Meta refreshes do pass some link juice, but are not recommended as an SEO tactic due to poor usability and the loss of link juice passed.
It is common practice to redirect one URL to another. When doing this, it is critical to observe best practices in order to maintain SEO value.
The first common example of this takes place with a simple scenario: a URL that needs to redirect to another address permanently.
There are multiple options for doing this, but in general, the 301 redirect is preferable for both users and search engines. Serving a 301 indicates to both browsers and search engine bots that the page has moved permanently. Search engines interpret this to mean that not only has the page changed location, but that the content—or an updated version of it—can be found at the new URL. The engines will carry any link weighting from the original page to the new URL, as below:
Be aware that when moving a page from one URL to another, the search engines will take some time to discover the 301, recognize it, and credit the new page with the rankings and trust of its predecessor. This process can be lengthier if search engine spiders rarely visit the given web page, or if the new URL doesn't properly resolve.
Other options for redirection, like 302s and meta refreshes, are poor substitutes, as they generally will not pass the rankings and search engine value like a 301 redirect will. The only time these redirects are good alternatives is if a webmaster purposefully doesn't want to pass link juice from the old page to the new.
Transferring content becomes more complex when an entire site changes its domain or when content moves from one domain to another. Due to abuse by spammers and suspicion by the search engines, 301s between domains sometimes require more time to be properly spidered and counted. For more on moving sites, see Achieving an SEO-Friendly Domain Migration: The Infographic.
Back when we launched our first website, seomoz.org, it was hosted at www.socengine.com/seo/ rather than on its own domain. When the original developers were moving seomoz.org to its own dedicated server, they wanted it to be accessed as its own domain rather than as a subdirectory of socengine.com. They needed visitors accessing anything in www.socengine.com/seo/ to be redirected to www.seomoz.org. The redirection had to accommodate several file and folder name changes and had to be done with 301 redirects in order to be search engine-friendly. They also needed to forward http://seomoz.org, too, for aesthetic purposes and to avoid canonicalization errors.
The simplest approach to do this would have been to add 301 redirects to the PHP code that powered seomoz.org using PHP's header function. Utilizing the power of the apache module mod_rewrite, however, the developers realized they could match specific patterns for entire folders and redirect them to their new URLs without having to go through every PHP script.
In order for this to work, a web server needs to have the apache module mod_rewrite installed.
Most apache installations will have mod_rewrite installed by default. SEOmoz's original server ran the Linux distribution FreeBSD and mod_rewrite was included by default. To check to see if the module is installed, a developer can verify it is working by adding the following line to the apache configuration file or to an applicable .htaccess file:
RewriteEngine On
The mod_rewrite module operates in per-server context or in per-directory context.
The per-server context requires that a developer must edit the apache configuration file, httpd.conf. The per-directory context uses .htaccess files that exist in each folder a user wants to configure. If a webmaster can not access httpd.conf, they will have to use .htaccess files.
From wikipedia.org:
A regular expression is a string that describes or matches a set of strings, according to certain syntax rules. Regular expressions are used by many text editors and utilities to search and manipulate bodies of text based on certain patterns.
Regular expressions are a valuable skill to learn for both programmers and systems administrators. To redirect URLs according to the examples in this document, it is important to understand the basics of using regexes. The following is a list of the characters and operators that are used in the regexes described in this document:
The original developers at Moz needed redirection from the old server to the new one with the filenames preserved.
Redirect: http://www.socengine.com/seo/somefile.php To: /somefile.php
Add the following directive to the applicable file on socengine.com's server:
RedirectMatch 301 /seo/(.*) / style="margin-top:0px">Content Goes Here
The regular expression /seo/(.*)
tells apache to match the seo folder followed by zero or more of any characters. Surrounding the .* in parenthesis tells apache to save the matched string as a back-reference. This back-reference is placed at the end of the URL that was directed to, in this case, style="margin-top:0px">Content Goes Here.
The original developers at Moz needed to redirect any requests that do not start with www.seomoz.org to make sure they included the www. They did this not only because it looks better, but to avoid common canonicalization errors.
Redirect: http://seomoz.org/
To: http://www.seomoz.org/
Redirect: http://mail.seomoz.org/
To: http://www.seomoz.org
Redirect: http://seomoz.org/somefile.php
To: http://www.seomoz.org/somefile.php
Add the following directive:
RewriteCond %{HTTP_HOST} *!^www*.seomoz.org [NC]
RewriteRule (.*) http://www.seomoz.org/ style="margin-top:0px">Content Goes Here [L,R=301]
This directive tells apache to examine the host the visitor is accessing, and if it does not equal www.seomoz.org, to redirect to www.seomoz.org. The exclamation point (!) in front of www.seomoz.org negates the comparison, saying, “If the host IS NOT www.seomoz.org, then perform RewriteRule.” In our case RewriteRule redirects them to www.seomoz.org while preserving the exact file they were accessing in a back-reference.
Several files that existed on the old server were no long present on the new server. Instead of preserving the file names in the redirection (which would result in a 404 not found error on the new server), the old files needed to be redirected to the root URL of the new domain.
Redirect: http://www.socengine.com/seo/someoldfile.php
To: http://www.seomoz.org/
Add the following directive:
RedirectMatch 301 /seo/someoldfile.php http://www.seomoz.org
Omitting any parenthesis, all requests for /seo/someoldfile.php should redirect to the root URL of http://www.seomoz.org
Some of the PHP scripts had different names but the GET string stayed the same. The Moz developers needed to redirect the visitors to the new PHP scripts while preserving these GET strings. The GET string is the set of characters that come after a filename in the URL and are used to pass data to a web page. An example of a GET string in the URL /myfile.php?this=that&foo=bar would be “?this=that&foo=bar.”
Redirect: http://www.socengine.com/seo/categorydetail.php?CAT_ID=12345
To: http://www.seomoz.org/artcat.php?CAT_ID=12345
Add the following directive:
RedirectMatch 301 /seo/categorydetail.php(.*) http://www.seomoz.org/artcat.php style="margin-top:0px">Content Goes Here
Once again the regular expression (.*) tells apache to match zero or more of any character and save it as the back-reference style="margin-top:0px">Content Goes Here. Since there is a style="margin-top:0px">Content Goes Here after /seo/categorydetail.php, it will now redirect the get string to this new PHP file.
In the original scenario there was a folder of files on the old server that were mixed HTML and PHP. On the new server these files were all PHP and needed redirect logic to change the old URLs to this new extension.
Redirect: http://www.socengine.com/seo/guide/anyfile.html
To: http://www.seomoz.org/articles/anyfile.php
Redirect: http://www.socengine.com/seo/guide/anyfile2.php
To: http://www.seomoz.org/articles/anyfile2.php
Add the following directive:
RedirectMatch 301 /seo/guide/(.*).(php|html) http://www.seomoz.org/articles/ style="margin-top:0px">Content Goes Here.php
(*.) matches zero or more of any character and saves it as the back-reference style="margin-top:0px">Content Goes Here. .(php|html) tells apache to match a period followed by either “php” or “html” and saves it as the back-reference (although this isn't used in this example). Notice the escaped period with a backslash. This is to ensure apache does not interpret the period as meaning “any character” but rather as an actual period. Enclosing “php” and “html” in parenthesis and separating them with a pipe “|” character means to match either one of the values. So if it were to say (php|html|css|js|jpg|gif) the regex would match any of the files with the extensions php, html, css, js, jpg, or gif.
By harnessing the power of mod_rewrite and a little regular expression magic the original developers at Moz developed a set of simple rules for redirecting web pages. By using 301 redirects, they did this in a way that was search engine–friendly.