How to redirect visitors?

When the domain of a site is changed or when the location of a page is changed, the previous URL must be redirected to the new location, due to backlinks and indexing by search engines. Several methods may be used to do that.
Note that for search engine only 301 redirects are valid and this require .htaccess or PHP.

Using .htaccess under Apache

A .htaccess file at the root of the site (or in a subdirectory if it concerns the contents thereof) will be taken into consideration by search engines. It will contain a line using the format:

RewriteEngine on
redirect 301 /directory/filename http://www.domainname.tld/directory/filename


RewriteEngine on
redirect 301 /mypage.html  

To redirect the site as a whole, define the root of the source site and the destination site, for example:

RewriteEngine on
redirect 301 /


RewriteEngine on
RewriteRule ^(.*)$ [R=301] 

If your account contains a "www" directory .htaccess is sotred into the "www" directory and may even be placed in subdirectories, with options to them. The scope is the directory and sub-directories.

The file must be in the Unix format.

Using frames

If the server (a Windows server, for example) does not recognize .htaccess or the "redirect" command a frameset allows for automatic redirection of the visitors. The index.html file contains the following lines, inside the body tag:

<frameset rows="*,0" cols="*" border="0">
  <frame src="" name="mypage" border="0"
     frameborder="NO" RESIZE scrolling="no">
  <frame src=""> 

Note that the name of site displayed in the URL field of the browser will always address that the user has typed.

Using JavaScript

<script language="JavaScript"> 
<noscript> <a href="">New URL</a> </noscript>

This works only if JavaScript is not disabled. The code is placed before the body section, which should contain a link to a new page, on which the user will click when JavaScript is disabled on their browsers.

Using PHP

If the page has the PHP extension and is processed by the interpreter, the old page can be redirected by replacing its content with this code:

   header("Status: 301 Moved Permanently"); 

The URL is that of the new page.

See also