RedirectADomain.com - How to set up a 301 Redirect?

Using a 301 redirect is best way to preserve your search engine ranking for a web page or domain name.  A redirect is typically used when the page or domain name is moved or replaced with another.

For example, a website say "RedirectADomain.ca" is very well-known and has a high ranking in the search engines.  The owner of the website decides to register the domain name "RedirectADomain.com" and use it as the primary domain name for accessing the website.  The owner is now concerned about how to notify not only the website visitors but also the search engines of the domain name change but still preserve the site's high search engine ranking.  Using the examples below the website owner is able accomplish two tasks, preserve the ranking and redirect website visitors to the new domain name.  This method is called a "301 redirect"; the 301 code is a HTTP status code which is interpreted as a "permanently moved".  Below you will find how to code a 301 redirect using popular scripting languages.

  • ColdFusion
  • PHP
  • ASP
  • ASP.net
  • JSP (Java)
  • CGI/PERL
  • Ruby on Rails
  • .htaccess

301 Redirection using ColdFusion

The code below will execute once the page is fully loaded in the web browser.  It is best to use the code below on a blank page so that additional items on the page doesn't delay the page load time.

ColdFusion

  1. <.cfheader statuscode="301" statustext="Moved permanently">
  2. <.cfheader name="Location" value="http://www.redirectadomain.com/">
* replace http://www.redirectadomain.com/ with your own domain name.

301 Redirection using PHP

The following PHP code must be run on a server which can interpret PHP coding - such servers include Apache Webserver as well as other Microsoft IIS (Internet Information Server).  Like all server-side languages, the server needs to interpret the PHP code before rendering it.  When the server gets to line 3 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

PHP (PHP: Hypertext Preprocessor)

  1. <?php
  2. Header( "HTTP/1.1 301 Moved Permanently" );
  3. Header( "Location: http://www.redirectadomain.com/" );
  4. ?>
* replace http://www.redirectadomain.com/ with your own domain name.

301 Redirection using ASP

The following ASP code must be run on a server which can interpret ASP coding - such servers include Microsoft IIS (Internet Information Server) as well as other non-Microsoft servers using alternative implementations such as Sun ONE, Arrowhead ASP or Cloudfoundry ASP.  ASP (Active Server Pages) is a programming language created by Microsoft in the late 90's.  ASP is sometimes referring to as Classic ASP or ASP Classic and was Microsoft's first server-side programming language.  Like all server-side languages, the server needs to interpret the ASP code before rendering it.  When the server gets to line 4 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

ASP (Classic Active Server Pages)

  1. <%@ Language=VBScript %>
  2. <%
  3. Response.Status="301 Moved Permanently"
  4. Response.AddHeader "Location","http://www.redirectadomain.com/"
  5. %>
* replace http://www.redirectadomain.com/ with your own domain name.

301 Redirection using ASP.net

The following ASP.net code must be run on a server which can interpret ASP.net coding - such as Microsoft IIS (Internet Information Server).  Like all server-side languages, the server needs to interpret the ASP.net code before rendering it.  When the server gets to line 5 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

ASP.net

  1. <script runat="server">
  2. private void Page_Load(object sender, System.EventArgs e)
  3. {
  4. Response.Status = "301 Moved Permanently";
  5. Response.AddHeader("Location","http://www.redirectadomain.com/");
  6. }
  7. </script>
* replace http://www.redirectadomain.com/ with your own domain name.

301 Redirection using JSP (Java)

The following JAVA code must be run on a server which can interpret JAVA coding - such as Tomcat.  Like all server-side languages, the server needs to interpret the JAVA code before rendering it.  When the server gets to line 3 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

JSP (JavaServer Pages)

  1. <%
  2. response.setStatus(301);
  3. response.setHeader( "Location", "http://www.redirectadomain.com/" );
  4. response.setHeader( "Connection", "close" );
  5. %>
* replace http://www.redirectadomain.com/ with your own domain name.

301 Redirection using PERL/CGI

The following PERL code must be run on a server which can interpret PERL coding - such as Microsoft IIS (Internet Information Server) or Apache Webserver.  Like all server-side languages, the server needs to interpret the PERL code before rendering it.  When the server gets to line 2 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

Perl (Practical Extraction and Report Language) or CGI (Common Gateway Interface)

  1. $q = new CGI;
  2. print $q->redirect("http://www.redirectadomain.com/");
* replace http://www.redirectadomain.com/ with your own domain name.

301 Redirection using Ruby on Rails

The following PERL code must be run on a server which can interpret PERL coding - such as Microsoft IIS (Internet Information Server) or Apache Webserver.  Like all server-side languages, the server needs to interpret the PERL code before rendering it.  When the server gets to line 2 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

Ruby or Ruby on Rails

  1. headers["Status"] = "301 Moved Permanently"
  2. redirect_to "http://www.redirectadomain.com/"
* replace http://www.redirectadomain.com/ with your own domain name.

301 Redirection using .htaccess

The following .htaccess directives must be run on a server which can understand .htaccess directives and have permission to override server configuration.  The .htaccess file is usually used on a Apache Webserver but can also work on Microsoft IIS (Internet Information Server) if configured to support .htaccess directives.  The directives act differently than server-side scripting because they are executed before any web pages or scripts.  When the server gets to line 3 below it will redirect the browser immediately.

.htaccess

  1. Options +FollowSymLinks
  2. RewriteEngine on
  3. RewriteRule (.*) http://www.redirectadomain.com/$1 [R=301,L]:
* replace http://www.redirectadomain.com/ with your own domain name.
Bookmark and Share