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.
You are here: home 301 redirects
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.
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.
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.
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.
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:
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.
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.
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.
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.
RewriteRule ^foldername/* http://www.domain.co.uk/ [R=301,L]
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:
Alternatively, this example redirects the www domain to the non-www if you wish to retain the non www domain:
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:
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.
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.
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:
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:
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.
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
ErrorDocument 404 /error404.htm
AddType application/x-httpd-php .htm
# rewrite stuff
# This is a comment, 24 April 2009, RT #4940
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
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/"
RewriteRule /(.*) http://www.mydomain.co.uk/$1 [R=301]
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:
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.
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.
This redirect method applies to Platforms with Active Server Pages:
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.
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.