Articles: Redirecting users

Because of the large number of request for help dealing with redirection it was about time I added an article about this at my website.
Added: 2003-08-01 16:40:54 - Modified: 2006-09-23 23:20:54 - Level: Beginner
Printer friendly version PDF File
Recommend this article to a friend.

Bookmark this on google Bookmark this on del.icio.us Submit this to digg Bookmark this at yahoo Bookmark this at reddit Bookmark this at furl Search technorati for links to this page Toggle more

In most cases you have 3 ways of providing the redirection. You can

  1. send a new header to the client
  2. use client side scripting
  3. use a META tag

Sending a new header is by far the optimal solution. It will work for any browser supporting HTTP (and what browser doesn't), and it will redirect search engines, etc. Using the Location header, eg

header('Location: http://www.new.host/');

in PHP. This will force a 301 or a 302 status message, 301 being a Permanent Redirect, and 302 being a Temporary Redirect. Redirection can also be accomplished by the web server directly, using mod_rewrite or equivalents.Using mod_rewrite you'd do:

RewriteEngine On
RewriteBase /
RewriteRule ^page1.html$ /page2.html [R=301]

Client side scripting got the drawback that it has to be enabled for it to function, 10-15% got JavaScript disabled by default, and VBScript is limited to Internet Explorer and there again got to be enabled. There are however some issues it handles better then using a META tag.

Using a META tag disables proper use of the back button, as opposed to using Client side scripting that can deal with this. The META approach uses a http-equiv.

<meta http-equiv="refresh" content="x;url=http://www.new.host">

X being the number of secs before redirecting.

The javascript option would be using e.g. self.location.replace('url'); but if you are to use this you would use this, it should be a combination of META-tag and JavaScript, with a delay on the javascript. The meta-tag should have a greater delay than the javascript, 3-5 seconds more.

If you got access to .htaccess files or otherwise got permission to modify the webserver's settings, or got access to server-side scripting or programming, go for approach number one. If you for some reason don't have access to any of this a combination of the two next approaches can be used.

Related articles:



[Sitemap]