CentrioHost Blog

Stories and News from IT Industry, Reviews & Tips | Technology Blog


How to setup a 301 Redirect

  • Category : cPanel Tutorials
  • Posted on : Apr 07, 2011
  • Views : 1,335
  • By : Ingavar J.

The “301 Permanent Redirect” is the most efficient and search engine friendly 
method for redirecting websites. You can use it in several situations, 
including:

  • to redirect an old website to a new address
  • to setup several domains pointing to one website
  • to enforce only one version of your website (www. or no-www)
  • to harmonize a URL structure change



There are several ways to setup a 301 Redirect, below I will cover the most 
used ones:

PHP Single Page Redirect

In order to redirect a static page to a new address simply enter the code 
below inside the index.php file.

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newdomain.com/page.html");
exit();
?>


PHP Canonical Redirect

The Canonical 301 Redirect will add (or remove) the www. prefixes to all the 
pages inside your domain. The code below redirects the visitors of the http://domain.com 
version to http://www.domain.com.

<?php
if (substr($_SERVER['HTTP_HOST'],0,3) != 'www') {
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.'.$_SERVER['HTTP_HOST']
.$_SERVER['REQUEST_URI']);
}
?>


Apache .htaccess Singe Page Redirect

In order to use this method you will need to create a file named .htaccess 
(not supported by Windows-based hosting) and place it on the root directory of 
your website, then just add the code below to the file.

Redirect 301 /old/oldpage.htm /new/http://www.domain.com/newpage.htm


Apache .htaccess Canonical Redirect 

Follow the same steps as before but insert the code below instead (it will 
redirect all the visitors accessing http://domain.com to http://www.domain.com)

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc] 


ASP Single Page Redirect

This redirect method is used with the Active Server Pages platform.

<%
Response.Status="301 Moved Permanently"
Response.AddHeader='Location','http://www.new-url.com/'
%> 


ASP Canonical Redirect
The Canonical Redirect with ASP must be located in a script that is executed in 
every page on the server before the page content starts.

<%
If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www."
& Request.ServerVariables("HTTP_HOST")
& Request.ServerVariables("SCRIPT_NAME")
End if
%>

Subscribe Now

10,000 successful online businessmen like to have our content directly delivered to their inbox. Subscribe to our newsletter!

Archive Calendar

SatSunMonTueWedThuFri
 12345
6789101112
13141516171819
20212223242526
27282930 

Over 20000 Satisfied Customers!

  • web hosting reviewer
    Valerie Quinn
    CTO, Acteon Group

    Centriohost staff were fantastic, I had a concern with a domain and they got back to me very quickly and they helped me to resolve the issue! ~ . . . Read more

  • Joomla hosting reviewer
    Collin Bryan
    Photographer, Allister Freeman

    I'm using centrio for my portfolio since 2006. The transition was seamless, the support was immediate, and everything works perfectly. ~ . . . Read more

  • dedicated server reviewer
    Harry Collett
    Actor, A&J Artists

    Very easy to understand & use even though I am not very technologically minded. No complications whatsoever & I wouldn't hesitate to recommend it to all. ~ . . . Read more

  • vps web hosting reviewer
    Porfirio Santos
    Technician, Diageo PLC

    Centrio support team have been amazingly responsive and helpful to any of my queries, thank you so much to the Centriohost have been amazingly responsive and helpful to any of my queries 👍👍👍 ~ . . . Read more

  • wordpress hosting plans reviewer
    Catherine Auer
    Doctor, SmartClinics

    Anytime I've had a problem I can't solve, I've found Centriohost to be diligent and persistent. They simply won't let an issue go until the client is happy. ~ . . . Read more

  • reseller hosting reviewer
    Effectivo Social
    Freelancer, Fiverr

    Recommend their shared hosting for all my SME web design clients. Their cloud or VME offerings are too great to deal with. Pricing is perfect and suitable for all users (͠≖ ͜ʖ͠≖) 👌 ~ . . . Read more

Top