301 Permanent Redirect


301 Permanent Redirect is used in several cases: when changing the domain, when transferring the site page, to glue the site name with www and without it. This is important for passing on Page Rank (PR) and retaining search traffic.

301 redirects are the best way to maintain your position in search engines when you migrate a page or site. Code "301" is interpreted as "moved permanently".

  • Simple redirect (in .htaccess file or httpd.conf for Apache):

    Redirect 301 / http://www.you.com/new.htm

    Redirect 301 is an instruction saying that the page has been moved
    / - means that everything from the top level of the site, including all subdirectories, will be redirected
    http://www.you.com/new.htm - new page or site (do not forget to put the last "/" if the redirect goes to the site).

    To redirect only a page, keeping the PR of the old page:

    Redirect 301 /old/old.htm http://www.you.com/new.htm
    /old/old.htm - path and name of the old page

    Similar syntax for site redirect:
    Redirect Permanent / http://www.you.com/

    Directory redirect example:
    RedirectPermanent /old-directory http://www.domain.com/new-directory/

    For example, we will redirect those who entered test to www.test.com, the rest to enter.test.com (the order of the entries is important):

    Redirect permanent /test http://www.test.com/
    Redirect permanent / http://enter.test.com/

    Note: for my purposes (changing the domain), the first version of a simple 301 redirect was enough.

  • Using mod_rewrite (written in the .htaccess file):

    The classic task of merging site names with and without www is solved as follows:

    Rewrite Engine on
    RewriteCond %{HTTP_HOST} ^yoursite\.com
    RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=permanent,L] .

    or alternative syntax:

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

    The designation [R=301,L] means: redirect the client and send him a 301 status code (R=301) and make this rule last (L).

    Redirecting an old domain to a new one:

    Options +FollowSymLinks
    Rewrite Engine on
    RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

    For example, if you want the rewrite.html file to be loaded instead of rewrite.htm, add to .htaccess:

    Rewrite Engine on
    RewriteBase /
    RewriteRule ^rewrite\.htm$ rewrite.html [R=permanent]

    To replace all .htm files with .html files:

    Rewrite Engine on
    RewriteBase /
    RewriteRule ^(.*)\.htm$ $1.html [R=permanent]

  • Redirect to PHP:

    header('HTTP/1.1 301 Moved Permanently');
    header('Location: http://www.newdomain.ru/newdir/newpage.htm');

    It's better to specify HTTP/1.1 as the older ones don't support shared hosting. Don't forget that before header is called, nothing should be output (e.g. echo or print). Therefore, it is better to put this code at the beginning of the php script. A more complete version of the php redirect with saving the transmitted page and call parameters:

    if ($ref) $ref='?'.$ref;
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: http://newdomain.com/'.$ref);

Last in our blog

Internet Marketing