How do I create a 301 redirect?


301 Redirect methods

To redirect requests for one website to another, you can simply utilise the 'Forwarding' function of Plesk. However, for Search Engine Optimisation purposes, a '301 redirect' is often required. A 301 redirect indicates to the web browser accessing the website, that it has been moved permenantly to the new location.

There are several different ways of implementing this;

 

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.

 

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.

 

 

Due to it's flexibility and ease of implementation, we recommend the PHP method for employing 301 redirect.

Last update:
2013-02-26 09:17
Author:
Nick Fawbert
Revision:
1.0
Average rating:0 (0 Votes)

You cannot comment on this entry

Chuck Norris has counted to infinity. Twice.

Records in this category

Tags

top