How to setup a 301 Redirect

By Daniel Scocco

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
%>



Share

83 Responses to “How to setup a 301 Redirect”

  • Togrul

    Thanks for such an informative post.

    It helped me much.

    Cheers,
    Togrul

  • Chris Lang

    Forgot to mention that you have done a great service to all blog users and that you have great resource on your site.

    Thank you and best wishes to you.

  • William Summers

    Great Tutorial. The PHP code worked great but I am having some serious issues with the ASP code for another site. When applied to the default.asp page I get the following error, Microsoft VBScript compilation error ‘800a0400’. So I removed the code so the site will work.

    Any ideas will be greatly appreciated.

  • Udegbunam Chukwudi

    Thanks for a nice tutorial. The PHP Single Page Redirect just came in handy as I created a custom 404 page for my blog and main site. ๐Ÿ˜‰

  • Chris Schwarz

    Hi,

    I am bringing in in a new website and plan on setting up 301 redirects in the .htaccess file.

    Do I need to keep the OLD .html pages on the server?

  • Shannon – SKS Designs

    I am currently using the following permalink structure and am looking to change it through a HTACCESS 301 redirect but cannot find the code that works.

    Currently permalink: /%year%/%monthnum%/%postname%.html
    Future permalink: /%postname%

    So, I want to remove the year and month from all posts and well as the .html ending.

    Can someone help me?

    • Udegbunam Chukwudi

      @Shannon – SKS Designs: I suggest you install Scott Yang’s Permalink Redirect plug-in. I use it on my blog and it works like a charm ๐Ÿ˜‰

  • pozycjonowanie

    Moved permanently is big part of SEO. It’s elementary know-how every SEO Specialist.

    Most important implementation in link building:

    * allow to collect linkjuice

  • Golland

    After hours on the phone with technical support it seems my hosting company won’t support the use of 301 re-directs.

    My site is on a Windows server and there doesn’t seem to be any way to make it work.

    Now looking for a new host that uses Linux servers and has better technical support!

  • Alexs

    After surfing online for an hour, I finally found an answer to my problem, tried it out and URL redirection worked like a charm. Unlike other sites I found, your article is concise, less words and straight to the point.

    Some readers might find this helpful, if your launching a new website or blog, plan from the beginning if you’re going to use the root of your website or if you’re going to use a subfolder. I installed my blog (wordpress) to a subfolder, and eventually changed my mind and want to move my blog to the root directory. Fortunately there is this URL redirection thing.

    Now I can finally use my main domain without typing any additional subfolders.

    Nice article thank you!

  • Email Tracking

    I have done canonical redirection in ASP.

    Google is treating my main page as “/” and also as “default.asp”

    I mean when I log in to webmaster tools, google is saying that I have a duplicate content on two pages and pages are “/” and “default.asp”

    Can anyone tell me why is it doing so?

  • wie bali

    Thank you for the tip ๐Ÿ™‚ Youรขโ‚ฌโ„ขve inspired me to bring my .htaccess file up to speed.

  • Mohamed Beshr

    very good and i will apply some of them to my site

  • Sameer Arora

    hi,

    this is really a nice URL

    I wonder if anyone could advise me on how to setup a 301 redirect page using html ?

    I have some URLs with .htm extensions indexed by Google
    I can only see php and asp stuff here

  • Bang Kritikus

    Thanks for your nice tips, Daniel.

  • Tarik

    When I use the .htaccess method, it redirects all my non-www traffic to the homepage. That’s a big mistake.

    I recommend using the PHP direct. Much cleaner. No .htaccess files to deal with, plus it’s a pitch and paste coding task. Redirect 100% better.

    Thanks for the tips, Daniel.

  • Dave Slade

    Hi

    Hey guess what i found out : Did you know that you can manually choose the domain page by going to googles webmaster page and chose the settings tab.

  • Dave Slade

    I have a problem
    my home page or domain is a pr3 but my index is a pr5, i thought these 2 pages were the same.

    How do i make all my links just go the domain or pr3 page.

    http://www.speakenglish.biz pr3
    http://speakenglish.biz/index.html pr5

  • Gary Conner

    The code to go the other direction is above under “PHP Canonical Redirect”.

    Thanks again!

  • Gary Conner

    I am trying to setup a 301 redirect so everyone going from http://www.NationalRealEstateBrokers.org will go to http://NationalRealEstateBrokers.org.

    Here’s the code to go the other direction:

    Does ANYONE know what is code is to do what I want to do and were the code needs to be placed???

    If you can help me, please email me at:
    Director@NationalRealEstateBrokers.org

    Thanks for your assistance!

  • Hendry Lee

    If you can avoid PHP or other backend level redirection if you have access to .htaccess. Not only will it be faster because you don’t have to start PHP every time someone enters or visit your blog or site with the “wrong” canonical name.

    Code-level redirection is necessary only if it is application specific, or not as permanent.

    http://blogbuildingu.com/articles/www-redirect-right-way

  • graphic art

    I tried doing it. But after several minutes my server went down. Internal server error. I removed the .htaccess file and it goes up again.

  • Get a free website

    thanks a lot for this tip. Will use it on all of my sites from now on.

  • Internet Marketing Leeds

    ..really useful and informative post.
    I have just successfully redirected a site by ammending a code that wasn’t working due to an error…thanks for sharing.

  • SEO Genius

    Yey ๐Ÿ˜€ been wanting to know how to do this for a while now

  • isaac

    do the redirecting through the net for me

  • Ashfame

    Thanks Daniel for the article and the help via IM. Got it done through .htaccess file.

  • shekhar suman

    well recently we moved to a new domain frm googlepages free server, now the worst part is googlepages doesnt support any of the methods stated above, only option left is meta refresh and loose my pagerank, any other solution i could not find till now, can any one of you help me out???????????????

  • Tom – Graphic Design

    Thanks for the useful info! I used your advice to easily setup a 301 on our design site!

  • Gilbert

    There’s an error on the ASP single page redirect.

    The line that reads Response.AddHeader= should not have the =

    Response.AddHeader=’Location’,’http://www.new-url.com/’ won’t work

    Response.AddHeader ‘Location’,’http://www.new-url.com/’ will work

Comments are closed.