SEO Resources

What Steve doesn't know about Search isn't worth knowing. He also has a great ability to impart his knowledge on to others in a friendly but highly professional training environment.

Andrew S.

Managing Director

SEO training courses at affordable pricesSEO training course

You are here: home 301 redirects

301 Redirect Examples

When updating any website with a new page structure, re-naming page URL's or changing the path of web pages by adding folders or altering folder names, many webmasters forget or simply don’t appreciate the SEO benefits of using Mod Rewrite or simple "301 redirects" to redirect old pages to new.

A different, but equally popular application for 301 redirects is to redirect all visitors to a certain website to another domain, either on a page by page basis or to redirect all pages on an old unused domain to a single page on a new domain - the homepage for example.

301's are often referred to as "Redirect Permanent", so as the name implies they are used to permanently redirect one web page to another - and are recommended by Google as the best method of implementing web page redirects. By contrast, a 302 Redirect is known as a "Redirect Temporary" and is commonly used for temporary web page redirection. 302 redirects being much less SEO friendly.

HTACCESS File

One easy way of creating Google-friendly page redirects is by creating an HTACCESS file and uploading it onto your Apache web server via a free FTP client like Filezilla. However, HTACCESS command syntax is complex and having any errors in your HTACCESS file can result in a 500 Internal Server Error rendering your website inaccessible. To help get things right, this page provides tips for creating 301 redirects for both static and dynamic web pages utilising the correct HTACCESS command line syntax. It also details how to stop image and content hotlinking using HTACCESS commands.

We cover some of the basics of Mod Rewrite below with examples of how to create and manage 301 Redirects using a .HTACCESS file on an Apache Web Server, including how to redirect dynamic pages.

SEO Benefits of 301 Redirects

Search engines place more emphasis on web pages which have received 'external votes' or links from other websites – particularly from sites which have authority and relevance.

Therefore, it is essential to either retain the existing page URL's when redesigning an existing website, or if a new URL page structure is implemented it is necessary to 301 redirect all of the old page URLs to the corresponding new pages to ensure that traffic and search engine ranking is not lost.

By implementing 301 redirects, the search engine value of any external links to the original page/s is retained and hence full search engine ranking is transferred to the new page – assuming that the new page is on the same domain.

A 301 redirect, when implemented properly also helps the search engines index the new page, whilst removing old content from the Google index faster.

Back to top

Usability Benefits

301 redirects are widely used to improve usability by preventing the 404 errors caused by removing web pages or renaming page URLs, by rewriting these pages to valid content.

Mod Rewrite Procedure & 301 Redirect Examples

If a large number of URLs require rewriting or redirection, then we recommend preparing an Excel spreadsheet tabulating all old page URL’s alongside where they will be redirected to. There are many free sitemap generators if you need a list of URLs to work against, including http://www.xml-sitemaps.com which will create a full HTML or XML sitemap quickly and easily, at no cost.

The method of redirecting web pages is dependent on the type of Web Server in use. Most Linux web servers run Apache and URL redirection is then done using Apache Mod Rewrite or simple 301 redirects, both of which are administered by means of an HTACCESS file. The use of Mod Rewrite requires the Mod Rewrite Engine to be installed on your Apache Web Server. To check this, simply create a file in Notepad containing the following code and save it as a file called test.php:

<?php phpinfo(); ?>
Download sample code

Then upload it to your web server and run it from a web browser by entering http://www.mydomain.com/test.php.

When PHP INFO runs, check that mod_rewrite appears in the list of "Apache loaded modules". If it isn't present, then contact your hosting company or web administrator to ask for an Apache configuration change.

Mod Rewrite rules are applied using an HTACCESS file. The HTACCESS file itself requires the use of correct command syntax and HTACCESS files are normally created using Notepad and then uploaded onto the root directory of your Web Server by FTP - the root directory being the same folder as your website homepage. To work correctly, the file must be saved as .HTACCESS, not as a .txt text file.

Please note that HTACCESS files will not work on a Microsoft Windows Web Server as they will not be read. To implement 301 redirects on a Microsoft Server requires the installation of a Mod Rewrite engine which runs under IIS to emulate Apache HTACCESS functionality – the IIS Mod Rewrite Engine from Micronovae being a good example.

Assuming your using an Apache Web Server, practical examples of HTACCESS 301 redirects to carry out specific tasks are given below.

Back to top

301 redirect using Apache Mod Rewrite

The example 301 redirect below rewrites the old page contact_us.html to contact_us.php and myoldpage.html to mynewpage.html. Rewriting further pages is simply a case of adding further rewrite rules as separate lines and then saving the file as .HTACCESS before uploading it to the root directory of the web server.

Note the use of "L" to depict the last HTACCESS rewrite rule.

RewriteEngine On
RewriteRule ^contact_us\.html$ http://www.mydomain.co.uk/contact_us.php [R=301]
RewriteRule ^myoldpage\.html$ http://www.mydomain.co.uk/mynewpage.html [R=301,L]
Download sample code

Redirect Old pages to new pages with a simple 301 redirect

In the following examples, page1.htm 301 redirects to page2.htm on http://www.mydomain.com. Note the need to include the full recipient domain with the new page. The second example redirects oldpage.htm (which is in a folder called 'myfolder') to newpage.htm which is in a folder called ‘newfolder’, again on http://www.mydomain.com.

RewriteEngine On
Redirect 301 /page1.htm http://www.mydomain.com/page2.htm
Redirect 301 /myfolder/oldpage.htm http://www.mydomain.com/newfolder/newpage.htm
Download sample code

Back to top

Redirect Folder (and Contents) to New URL

To redirect a folder and all of the pages or files contained within it to another page or domain, use the following HTACCESS command syntax.

RewriteEngine On
RewriteRule ^foldername/* http://www.domain.co.uk/ [R=301,L]

Canonical Domain Re-Writing

Domain canonicalisation is highly undesirable for SEO. This is where the canonical URL of any domain http://yourdomain.com is accessible as well a the www domain http://www.yourdomain.com when entering it into a web browser.  In this case search engines treat these as two different domains and it can dilute the value of external links to a domain by splitting the link value between the www and non-www domain variants.

Canonicalisation can cause search engine indexing problems in extreme cases, where the canonical domain effectively fights against the www domain for search prominence. It is therefore best to Mod Rewrite 301 redirect the unwanted canonical domain to the domain you wish to keep (in this case the www site). Here is an HTACCESS Canonical rewrite example which redirects http://mydomain.co.uk to http://www.mydomain.co.uk:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
Download sample code

Alternatively, this example redirects the www domain to the non-www  if you wish to retain the non www domain:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^mysite\.co.uk$
RewriteRule (.*) http://mysite.co.uk/$1 [R=301,L]
Download sample code

Dynamic URL Rewrite Example

When rewriting dynamic pages, Rewrite Conditional should be used to check for the dynamic query string (in this case "?view=home" before implementing the rewrite of index.php to the root top level domain in this case.

Please remember to remove the ",L" from the code if this isn't the last 301 redirect in your HTACCESS file.

An example is given below:

RewriteEngine On
RewriteCond %{QUERY_STRING} view=home
RewriteRule ^index\.php$ http://www.domain.co.uk/$1? [R=301,L]
Download sample code

Redirect All Pages to Another Domain

The following HTACCESS code redirects all pages and directories on www.olddomain.co.uk to http://www.mydomain.com. This is useful if you wish to redirect every page on a particular website to one recipient page – like the homepage of another site.

Warning
Redirecting pages from an established domain with good Google ranking to a brand new domain will result in much reduced Google ranking for a period of up to 12 months. If an entire domain is redirected in this manner, up to 60% of Google traffic can be lost whilst the Google Trustrank of the domain improves.

This problem is less likely to occur if pages are redirected to a similarly aged and ranked domain.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.olddomain\.co\.uk$
RewriteRule ^(.*)$ http://www.mydomain.com [R=301,L]
Download sample code

Image and Content Hotlinking - Bandwidth Theft

Hotlinking and bandwidth theft is a major problem nowadays and we've recently noticed our Google Analytics statistics show several domains which are hotlinking to our content and images without our permission.

To combat this it is possible to prevent any domain from being able to fetch content from your Apache web server, returning instead a 403 Forbidden Error. The following .HTACCESS code will prevent a domain from being able re-use your content as a feed:

RewriteEngine on
RewriteCond %{HTTP_REFERER} http://www\.domain-to-be-removed\.com [NC]
RewriteRule .* - [F]

The following code prevents external domains from hotlinking to images on your site. Image hotlinking results in server bandwidth theft and increased web server loading. Just replace "Mysite" with your domain name and then create an image to display to hotlinkers, naming that "hot-link.gif". Place this on the domain you intend to redirect to, changing "domain.com" accordingly:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://domain.com/hot-link.gif [L]

Wildcard Redirects

It is possible to set up redirects which redirect a group of page URL's that have some similarity such as being in a common folder or which have a common word somewhere in the URL to a new page. An htaccess wildcard rule is used for advanced 301 redirects like this.

Examples are given below. In the first case all pages inside a particular folder called 'foldername' ending in the same keyword are redirected. The wildcard matches any character combinations in the first part of the URL after the folder name, but the "_keyword" must be present in a URL for the redirection to take place as must the foldername. In the second example a URL in the root folder is redirected without matching a folder name.

You can modify these 301 redirect examples to suit your requirements and even add multiple wildcards if needed.

Inside a particular folder:

RewriteEngine On
RewriteRule ^foldername/(.*)_keyword.html$ http://www.website.co.uk/ [R=301,L]

Inside root folder:

RewriteEngine On
RewriteRule ^(.*)_keyword.html$ http://www.website.co.uk/ [R=301,L]

301 Redirect - URL's With Spaces

301 redirecting URL's with spaces in them turned out to be tricky. The HTACCESS file 301 rewrite method was unreliable, so in the end a fully working solution was found by adding the rewrites to the web server config file using an SSH connection.

Note the back-slash is used before the spaces in the URL's to be re-written. If you're lucky this URL convention may work when used in a HTACCESS file using rewrite rules.

Here is an example of the code used in the Server Configuration file:

# mydomain.co.uk vhost added on Tue Jan 31 09:43:27 GMT 2006

<VirtualHost *:80>
ServerAdmin hostmaster@mydomain.co.uk
DocumentRoot /home/www-data/mydomain.co.uk/docs
ErrorDocument 404 /error404.htm
ServerName mydomain.co.uk
ServerAlias www.mydomain.co.uk
AddType application/x-httpd-php .htm
# rewrite stuff
RewriteEngine on

# This is a comment, 24 April 2009, RT #4940
RewriteLog /tmp/loc
RewriteLogLevel 2
RewriteRule ^/page1\.htm$ http://www.mydomain.co.uk/ [R=301]
RewriteRule ^/page2\.htm$ http://www.mydomain.co.uk/newpage2.htm [R=301]
RewriteRule ^/page\ with\ spaces\.html$ http://www.mydomain.co.uk/newlocation.htm [R=301]
RewriteRule ^/page\ with\ spaces2\.html$ http://www.mydomain.co.uk/newlocation2.htm [R=301,L]
# End of RT #4940
ErrorLog /home/www-data/mydomain.co.uk/logs/error_log
CustomLog /home/www-data/mydomain.co.uk/logs/access_log combined
ScriptAlias /cgi-bin/ "/home/www-data/mydomain.co.uk/cgi-bin/"
Alias /stats/ "/home/www-data/mydomain.co.uk/stats/"
</VirtualHost>

<VirtualHost *:80>
ServerAdmin hostmaster@mydomain.co.uk
ServerName mydomain.com
ServerAlias www.mydomain.com
RewriteEngine On
RewriteRule /(.*) http://www.mydomain.co.uk/$1 [R=301]
</VirtualHost>

Custom Error Page (404)

Htaccess offers a convenient way to create a custom error 404 page, which is displayed whenever your Web Server receives a request to render a non-existent page. This has certain SEO and traffic retention benefits too as many forums truncate URLs, so custom 404 handling will effectively redirect 404 pages to a dedicates URL, which can be your homepage or sitemap for example. Example code is below:

ErrorDocument 404 /error404.htm
Download sample code

Back to top

Alternative Forms of Redirects

  • Meta Refresh

    This rather undesirable method of redirecting a web page uses a Meta tag which is placed inside the <head> part of the web page. Using Meta Refresh is not recommended as it was widely exploited a few years ago by spammers who set up highly optimised, keyword rich pages and set them to redirect to an alternative domain with no delay – thereby creating what are often referred to as "Doorway pages". For this reason search engine penalties can be triggered by the inappropriate use of Meta Refresh, particularly when the redirect time is set to sub 5 seconds. The example below the delay time is set to 6 seconds and the page redirects to http://mysite.com.

    <meta http-equiv="refresh" content="6;url=http://www.mysite.com/" />
    Download sample code
  • PHP 301 Redirect

    Insert the following code into your web page to create a 301 PHP header redirect. Note that to run PHP in a .htm or .html extension file requires modification of the default Apache Web Server settings, so the use of this as a redirect method is normally restricted to files with the .php extension.

    <?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.mynewdomain.com/page.htm");
    exit();
    ?>
    Download sample code
  • ASP 301 Redirect

    This redirect method applies to Platforms with Active Server Pages:

    <%
    Response.Status="301 Moved Permanently"
    Response.AddHeader='Location','http://www.mysite.com/'
    %>
    Download sample code

Testing 301 Redirects

In order to test that the Htaccess file is working and that each page is redirecting correctly, we recommend testing each 301 redirect using a Web Page Header Check on the redirected page URL's. The returned headers should show 301 redirects are in place for each redirected URL in the HTACCESS file.

Disclaimer
The information contained in this Mod Rewrite article is correct to the best of our ability. No guarantee of accuracy is provided or that any code snippets will work on all web server configurations.

Get professional help with 301 redirects