This tutorial describes how to properly redirect a web page using an HTTP 301 status code and Location header. The 301 status code is used to indicate that a page has permanently moved.
- I. Redirects with Server Side Scripting;
- II. Redirecting with .htaccess;
- III. SSL https to http;
- Why should I do this?
- Disclaimer.
I. Redirects with Server Side Scripting
If you have a dynamically driven (server side) web site, you can implement one or more of the following scripts, depending on which scripting language(s) you use.
These portions of code are fairly self-explanatory. They simply provide the appropriate 301 response header, and the location of the new page.
HTTP 301 Redirect in ASP-VBScript
Include the following ASP code at the top of each page:
<%@ Language=VBScript %>
<%
' Permanent redirection
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", "http://www.dexterityunlimited.com/"
Response.End
%>
HTTP 301 Redirect in ColdFusion
Include the following ColdFusion code at the top of each page:
<CFHEADER statuscode="301" statustext="Moved Permanently">
<CFHEADER name="Location" value="http://www.dexterityunlimited.com/">
HTTP 301 Redirect in PHP
Include the following PHP code at the top of each page:
<?php
// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.dexterityunlimited.com/");
exit();
?>
HTTP 301 Redirect in Perl
Include the following Perl code at the top of each page:
#!/usr/bin/perl -w
use strict;
print "Status: 301 Moved Permanently\n";
print "Location: http://somewhere/page.htm\n\n";
exit;
HTTP 301 Redirect in JSP/Java
Include the following Java code at the top of each page:
<%
response.setStatus(301);
response.setHeader( "Location", "http://www.dexterityunlimited.com/" );
response.setHeader( "Connection", "close" );
%>
II. Redirecting with .htaccess
If you are on an Linux/Apache server, or a Microsoft Windows Server that can run Apache modules, you can do your redirects with an .htaccess file.
The .htaccess file is a very powerful tool, but can wreak havoc on a site if not implemented correctly. Always save a backup of any existing .htaccess file before attempting any changes.
Redirection with mod_rewrite
The following lines redirect the client to a new location. In this case, a request for the file contact.php is sent to the file contact-us.php:
rewriteEngine on
rewriteRule ^contact\.php$ http://www.dexterityunlimited.com/contact-us.php [R=permanent,L]
Redirect all non-www traffic
The code below will direct all traffic which does not contain the www in the URL to the same page, but now including the www. And that will clean up the canonicalization problem that arises when the www version of your site gets indexed along with the non-www version:
rewriteCond %{HTTP_HOST} ^dexterityunlimited\.com$
rewriteRule ^.*$ http://www.dexterityunlimited.com%{REQUEST_URI} [R=permanent,L]
Redirect aliases of home page to the root website
Have you noticed in your log files that people are looking for index.htm or index.asp in your root folder? Instead of letting them get a 404 Not Found error, why not direct them to your root folder, without a filename:
rewriteRule ^index\.(php|html|htm|asp) http://www.dexterityunlimited.com/ [R=permanent,L]
III. SSL https to http
Have you experienced serious canonicalization problems when the secure https version of your site was fully indexed along side your http version? If that is your case, there is a way to redirect https for the bots, telling them bots not to index the https version.
Our tutorial Search Engine Indexing of Secure Pages describes how to properly implement search engine friendly redirects using an HTTP 301 status code and Location header.
Why should I do this?
I am certain there are some of you asking this question. The answers are many, depending on your particular situation.
Are there links to your site that are outdated, but you have no way to change them? Simply create a redirect that will take visitors using that outdated link to the most appropriate new page on your site.
Is there a link in a directory somewhere that is misspelled, or is missing the www? Use one of the methods above to correct the situation at the server.
There are many ways for you to control what people see and where they can go on your web site. Take some time to learn a few tricks, and you might find that your traffic increases because you aren’t leaving anyone behind.
0 comments:
Post a Comment