Guides

Status 301 - Moved Permanently

How to make a web page 301 redirect

If the address of a page that has already been indexed by search engines is changed, the page's ranking usually worsens. The 301 Moved Permanently HTTP header, which notifies both the search engine bot and the browser that a page has been moved permanently, helps to preserve the ranking. Here are the most common methods to implement it.

  • Redirect in ASP

    Suppose we want to replace the page oldpage.asp with the page newpage.asp on the website www.domainname.it. to ensure that the old page responds with a redirect 301 to the new page, simply replace the code of oldpage.asp with the following:

    oldpage.asp

    <%
    Response.Status = "301 Moved Permanently"
    Response.AddHeader "Location", "http://www.domainname.it/newpage.asp"
    %>
  • Redirect in PHP

    Suppose we want to replace the page oldpage.php whit the page newpage.php on the website www.domainname.it. To ensure that the old page responds with a redirect 301 to the new page, simply replace the code for vecchiapagina.php with the following:

    oldpage.php

    <?php header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.domainname.extension/newpage.php");
    exit();
    ?>
  • Redirect with .htaccess file

    Suppose we want to replace the page oldpage.html on the website www.domainname.it with the page newpage.html. To ensure that the old page responds with a redirect 301 to the new page, simply add the following line to the .htaccess file:

    .htaccess

    RewriteEngine On
    RewriteRule ^oldpage.html$ http://%{HTTP_HOST}/newpage.html [R=301,L]
    

    With the .htaccess file, it is possible to redirect an entire folder or even an entire site, as shown below:

    .htaccess

    RewriteEngine On
    RewriteRule ^(.*)$ http://%{HTTP_HOST}/newpage.html [R=301,L]
    

NOTES

  • The destination address of the redirect does not necessarily have to belong to the same domain.
  • With the .htaccess file, a 301 redirect can be made to any URL and any document.