Ars Informatica
August 05, 2020
Health Care Informatics
Web-based MySQL/PHP Databasing
Web Development
Favourite Software
Hardware for the Frugal Fanatic
Graphic Design and Image Processing
Free Scripts and Software
About Us
Contact Us

 Article Feed for this site

Web page redirection via 301 .htaccess, PHP, ASP, Java, CGI, HTML and Javascript Redirects

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

It is that difficult. The 301 advises browsers and site search engines that the page has Moved Permanently. Search engines will revise their links, but leave your rankings intact. And you don't rely on a particular browser's HTML or Javascript implementation or configuration.

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. ''.

Another example:

Redirect 301 /old/index.htm
Redirect 301 /old/index.html

Directories are equally easy to redirect, i.e.:

Redirect 301 /you

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 non-Apache servers, or if you don't have access to the .htaccess file, 301 Redirects are still the preferred mode of web page redirection. Same reasons as before: safe, effective, and leaves page rankings intact. No revisions to HTML necessary; not dependent on Javascript.

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', ''


response.setHeader('Location', '');
response.setHeader('Connection', 'close');

And for CGI/PERL:

$q = new CGI;
print $q-> redirect('');

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
<a href=""></a>.

Do provide a link in your BODY text - not all browsers may handle the redirect properly.

Javascript Redirects

Javascript redirects are subject to the same problem as HTML redirects. These have been abused by spammers for years, and search engines now either downgrade or de-index pages linked in this fashion. Add the following to the HEAD section of your HTML page:

<script language="JavaScript">
window.location = '';
// -->


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.

Copyright © 2020 Ars Informatica. All Rights Reserved.