January 31, 2007
Manage a web site long enough, and you'll change your layout...
You may have database-driven web pages, or some other form of dynamic content. Your index.htm has become index.php or index.asp. You've changed your directory structure as the site has grown. But you don't want to break the links others have made to your site, or get dropped by search engines because they can no longer find your material, or even recode your own pages' links to accommodate those changes. You need to redirect.
The 301 Redirect: simplest and best
The most intimidating web page redirects involve revising the .htaccess files that regulate how Apache web servers redirect page requests. Intimidating, but not difficult. If you manage your own server, or your web host allows you to edit and post the .htaccess files, redirection couldn't be easier ...
An .htaccess file could contain just a single line, i.e.
Redirect 301 /index.htm http://www.your-site.com/index.php
The file is edited using a plain-vanilla text editor, and must be saved in plain-text format: formatting codes will screw it up. After the 'Redirect 301', reference the file you are redirecting from, using its name and full directory path, i.e. /oldroot/oldsubdir/index.htm. The last entry on the line consists of the web page's full URL, i.e. 'http://www.your-site.com/new/index.php'.
Redirect 301 /old/index.htm http://www.your-site.com/new/index.php
Redirect 301 /old/index.html http://www.your-site.com/new/index.php
Directories are equally easy to redirect, i.e.:
Redirect 301 /you http://you.your-site.com/personal
Save the edited file to your site's root directory. Finally, if an .htaccess file is already present, make sure that you edit it rather than delete and replace - it likely contains instructions necessary to your site's proper functioning.
301 Redirects using server-side code
For PHP, replace the previous page's code with:
header('HTTP/1.1 301 Moved Permanently');
header('Status: 301 Moved Permanently');
exit; //terminate program; optional but recommended
For ASP redirects, use the following:
<%@ Language=VBScript %>
Response.Status = '301 Moved Permanently'
Response.AddHeader 'Location', 'http://www.your-site.com/new/index.php'
And for CGI/PERL:
$q = new CGI;
print $q-> redirect('http://www.your-site.com/new/index.php');
HTML Redirects: easy and bad
HTML redirects have a role. In my opinion, only one useful role: directly advising site visitors that the page address has changed, prior to redirecting them. However, because of their misuse by spammers, search engines will often de-index the original sites, and not index the new ones. Even if de-indexing does not occur, search engines downgrade site rankings after HTML redirection.
While I do not recommend HTML redirection, here's how. To the HEAD, add:
<META http-equiv="refresh" content="5;
Where the content attribute specifies the number of seconds before the redirect takes place. In the page's BODY, you'd advise your readers of the change in web location, i.e.
This page has moved.
<p>If your browser does not redirect in 5 seconds, follow the link to
Do provide a link in your BODY text - not all browsers may handle the redirect properly.
window.location = 'http://www.your-site.com/new/index.php';
A final note: while all of the above code has worked for me, if you muck up your .htaccess file, you may not be able to access your web site properly, after. I am not responsible for any problems you might encounter; use at your own risk. Be careful. Do your research. Back up your data, test before upload, and test again.