301 Redirection Code for Mod_Rewrite, PHP, ASP, .NET, Perl and ColdFusion Scripting Languages

One of the most overlooked elements in proper web site coding is domain redirection. It's important to avoid duplicate content issues by making sure all web requests point to one destination. The most common error I find is web sites having both site-xyz.com and www.site-xyz.com showing the exact same content whether surfing the with or with the www. The following methods can be used to solve this problem and make your sites more search engine friendly and create a better experience for your users. Simply select the method that best matches your particular web environment below:

Redirect with .htaccess and Mod_Rewrite

The following code snippet is an example of utilizing the .htaccess file to handle domain and subdomain redirection. This method can be used to both temporarily, via 302 redirect method, as well as permanently redirect via 301 redirection. The .htaccess file is only used within Linux web server environments; however, the method will work 99% of the time regardless of whether you host your own servers or manage your web site hosting with a third-party vendor.

301 Redirect: xyz-site.com to www.xyz-site.com

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.xyz-site.com$ [NC]
RewriteRule ^(.*)$ http://www.xyz-site.com/$1 [L,R=301]

301 Redirect: www.xyz-site.com to xyz-site.com

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^xyz-site.com$ [NC]
RewriteRule ^(.*)$ http://xyz-site.com/$1 [L,R=301]

301 Redirect: Redirecting Individual pages

Redirect 301 /previous-page.html http://www.xyz-site.com/new-page.html

* These scripts should be placed in the .htaccess file. I chose to expose these .htaccess methods first since they are most flexible and powerful of them all. These examples alone serve as a great case for hosting on Linux web servers.

Redirect with PHP

<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.xyz-site.com" );
exit(0);
?>

* This script should be the first piece of code on the page to function correctly.

Redirect with classic ASP

<@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently" Response.AddHeader "Location",
" http://www.xyz-site.com"
%>

* This script should be the first piece of code on the page to function correctly.

Redirect with ASP.NET

<script>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.xyz-site.com");
}
</script>

* This script should be the first piece of code on the page to function correctly.

Redirect with ColdFusion

<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="http://www.new-xyz-site.com/">

* This script should be the first piece of code on the page to function correctly.

Redirect with Perl

$q = new CGI;
print $q->redirect(" http://www.new-url.com/ ");

* This script should be the first piece of code on the page to function correctly.

A Word of Caution Regarding META Refresh & Javascript

META refresh, javascript and other non-301 methods available for redirecting web pages should be avoided at all costs. These methods have been used for cloaking and malicious redirection for many years and can equate to getting your site banned from inclusion within the search engines. I know from experience as phatz.com was hacked recently and the hacker redirected the phatz.com home page using javascript redirection. The page would still load for me, but the home page was kicked out of Google. I'm sure the intent was to get my entire domain banned, which didn't happen, but it did remove the site from index for quite awhile. Be aware. Be smart. Never stop learning.

phatz.com codes in valid, strict XHTML and CSS | Specializing in responsible SEO Training in Atlanta. phatz.com's search study group, SEO Atlanta is specifically dedicated to SEO training, education and practice.

For high school student athletes looking to be recruited: Real Deal Recruits - Are you the real deal?