The 301 redirect is an efficient means of webpage redirection, to be used when you have redesigned your website. This HTTP code stands for 'moved permanently'. The 301 redirect can be implemented by creating a .htaccess file in Apache servers.

The process to create 301 redirects depends on the web server being used.

The Meaning of .htaccess File

In Apache web servers, a check is done in a .htaccess file each time a visitor or spider sends across a request for a web page. This file has instructions directed at particular requests, which could include security, redirection issues and serving error pages.

The .htaccess file is situated in the root of the web server.

Below is an example of how a particular page can be redirected using an .htaccess file in an Apache server:

Redirect 301 /old.html http://www.newsite.com/new.html

Redirects are also used by websites to redirect users from the non-www versions of their pages to the www version of their pages, or vice versa.

The code below should be placed in the .htaccess to achieve the above redirection.

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

The .htaccess file should be placed in the root of your old website.

Redirecting Web Pages in IIS

Use the following steps to redirect web pages using IIS:

  1. Go to the run command in Windows (Windows Logo + R), type inetmgr and then click Run.
  2. Right click on the name of the folder or the file that you want to redirect, and then click Properties.
  3. Under the Directory tab, check the radio button for 'A redirection to a URL'
  4. Type in the URL of the redirection page.
  5. Check the two options, 'The exact URL entered above' and 'A permanent redirection for this resource' and then click Apply.

301 Redirect Within Individual Pages

301 Redirect in ASP-VBScript

If your website is running on IIS, you can use the following VBScript code in your ASP pages to perform the 301 redirect:

<%@ Language=VBScript>
<%
' Permanent redirection
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", "http://www.somacon.com/"
Response.End
>

301Redirect in PHP

The code below will help you achieve 301 Redirect in PHP:

<?php
// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.somacon.com/");
exit();
?>

301 Redirect in Perl

The code below will help you achieve 301 Redirect in Perl

#!/usr/bin/perl -w
use strict;
print "Status: 301 Moved Permanantlyn";
print "Location: http://somewhere/page.htmnn";
exit;

301 Redirect in ColdFusion

"301" statustext="Moved Permanently"
"Location" value="http://www.somacon.com/"