Redirects on a Website

These redirects assume an Apache server and work in the .htaccess file. It is placed at the root of the site and can be overridden in sub-directories. PHP commands may be used too.


Redirect vs. URL-rewriting

We must distinguish the principle of redirect to that of URL-rewriting. The two techniques are similar but have an opposite effect:

Redirect: The URL given by the user is redirected to a new page. The URL of the new page is displayed by the browser.
It is used for a change of address. The new URL should be indexed by search engines and the older removed.

URL-rewriting: The URL given by the user is invisibly replaced by the server by a new path. However, the browser displays the URL given by the user.
It is used to separate the visible URL of the actual path and to provide users more convenient labels. Only the visible URL must be indexed by search engines and not the path on the server.

Apache uses the same RewriteEngine module in both cases, but the Redirect command or 301 option change the URL displayed. In fact, the server sends the new URL to the browser that must make a new request.

Redirects should be placed before rewritings in .htaccess.

How to add www

How to redirect to

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

HTTP_HOST represents the domain so the condition extracts the domain from the URL and compare it with a string that starts by
And we do a 301 redirect by replacing with for all URLs that match the condition.

How to redirect 404 errors on one page

This may be an error page, called page 404 that is the code returned when a page is not found, or the home page (most useful).

RewriteEngine  on
ErrorDocument 404 /index.php

Note that the directive "RewriteEngine on" that activates the redirects is put once at the beginning of the .htaccess file.

Redirect a domain name to another

To change definitively the domain, the new domain beeing for example With .htaccess (on the Apache server)

RewriteEngine  on
Redirect 301 /  

All pages will be redirected to new files with the same name but with a different domain.

For a temporary change you would use the code 302 instead.

To redirect a domain name on a site with PHP, you can use the following code in the index.php file:

   header("Status: 301 Moved Permanently"); 

For the visitor it is as if he had typed the URL of the destination site.

For a temporary change, it is simple as ot is the default, so it is not required to set a status:

<?php header("Location:"); ?>

How to redirect the home page to the name of the site

We would like the URL as be redirected by the server to to prevent robots from search engines do see two different URLs where there is only one.

In this case, we will use the PHP environment variable REQUEST_URI which indicates which page has been requested to the server. Then we use a load command, as in the following example:

 if(eregi('index.(html|php)', $_SERVER['REQUEST_URI'])) 
  header('HTTP/1.1 301 Moved Permanently'); 

We checked with the function eregi that the loaded page is index.php or index.html (or any other name) and only in such cases is made the redirection.

Another option in .htaccess is to rename the home page, home.html for example, and define it as home page with the following command:

DirectoryIndex home.html

Redirect on a moved page

A page has been moved to a new directory, or under a new name (or both), this directive must be given to the server:

Redirect 301 /directory/page.html  

Whether on the same site or on another, the site name is always included in the destination URL, and not in the source.

Redirecting a whole directory to a single file

If you want to remove a sub-directory from the Web, this can be achieved with a single command.

RedirectMatch 301 ^/mysubdir/.*$

The directory to remove is called "mysybdir" and the target is the index of another directory or a page.
The target must not be included in the directory to redirect because that will let enter an infinite loop.

In terms of SEO, such an operation is disadvantageous. It is better to place a page in "noindex" rather than redirect to another page when the content is different.

It you want a redirect, as always, you must ensure that all links pointing to a redirected page are updated. A link to a redirect page on the same site let it penalized.
Use the link checker with options -r -s -f to find these links.

Reference: The Apache Manual.