About redirects

The web change constantly and content moves around. This article gives a brief overview of the various different options available to you, to make a redirect. Redirects come in two major flavors – clientside and serverside.

Client side

Clientside redirects reside within the HTML documents on the server. There are three basic ways of making these:

    • Manual (or user driven) redirects.
    • Meta headers.
    • Javascript redirects.

Manual

The manual redirects most often used when you really, really wants the users to know, that the page has moved. Instead of doing an automatic redirect, you create a common html page, which informs the user that the page has moved, and provide a link to the new page. In some cases, this redirect-type exists in combination with a timed auto-redirect that upon timeout do the redirect.

The simple version could look like this:

<html><head>
<title>page moved</title>
</head><body>
  Please go to the new <a href="http://www.example.com/">destination</a>!
</body></html>

and the advanced version is in the next paragraph.

Meta headers

Meta http-equiv headers were a new thing in HTML 3.2, and provide a way to “pseudo provide http-headers”. Besides providing content-encoding headers and other stuff, this gives you a way to provide a refresh header. A the header is called refresh and is inserted like this:

<html>
<head>
  <meta http-equiv="refresh" content="0; url=http://www.example.com/">
</head>
</html>

In the example above, you would instantly be redirected to the destination – www.example.com. You could change the zero to another value an get a timed auto-redirect. Here’s an example

<html><head>
  <meta http-equiv="refresh" content="10; url=http://www.example.com/">
</head><body>
  Please go to the new <a href="http://www.example.com/">destination</a>!
</body></html>

Javascript

Doing redirects in javascript is really simple. The easiest way is attaching the needed javascript to the onLoad-handler.

<html>
<body onload="document.location='http://www.example.com/'">
</html>

The javascript redirects, is most commonly used on buttons or other user-triggered events, and less common as just redirects.

Server side

Every time a url on a sever is called, the webserver issues a http-code along with the answer/content it provides back to the browser.
These codes are usually hidden from sight, but in some cases, browsers (and other agents accessing the site) use these headers. The http-codes are divided into classes, and one of these classes (the 3xx codes) is dedicated to various redirection codes.

A redirect is not just a redirect. Some of the different redirect codes include:

  • 301 – moved permanently
  • 302 – Temporary moved
  • 303 – See other (can redirects of POST requests)
  • 307 -Temporary Redirect

If the page the user asks for really is moved to a new location, you should probably use the 301 code in most cases.

For more information on http-codes see RFC 2612 – section 10.

In the WebServer (apache)

Supposing you’re using Apache (and most other webservers), you can usually provide redirect-instructions in the webserver configuration file. In Apache, the directive is called “Redirect” (no surprise there), and is issued by adding a line like this to the httpd.conf file:

Redirect /example/page.html http://www.example.com/page.html

or issue a 301-header and thus telling the page was moved permanently:

Redirect 301 /page.html http://www.example.com/page.html

If you don’t like http-codes, your could make it slightly friendlier by using:

Redirect permanent /page.html http://www.example.com/page.html

Adding redirect instructions to you httpd.conf file, should probably only be done to a limited extend. If you need more that a few, you should probably look into rewrite maps or even a serverside script to do the redirect.

If you need to do advanced redirects the mod_rewrite module for apache can do just about anything including redirects based on regular expressions and other rocket science stuff. Do note, that mod_rewrite, while being powerful, does require great skills to master.

Rewrite maps

For a few redirects, the
In the httpd.conf (probably in the correct VirtualHost section) add a line like this:

RewriteMap examplerewrites txt:/location/on/your/server/redirect.txt

Then create a file (in the location specified) with the redirects. The file should consist of one line for each redirect (releative path for that Virtualhost) a space and the full URL of the destination you want to redirect to. If you want to do massive redirects, you should probably not use txt-format redirect files, but instead use Hash (dbm) files for mapping. See the documentation for mod_rewrite for details.

In php

With PHP (and most other server-side languages) you have the option to send http-headers to the browser. To do a basic redirect:

<?php
  header('Location: http://www.example.com/');
?>

Since you can provide all the headers you want (provided no headers have already been sent), you could even send a 301 (Permanently moved) code like this:

<?php
  header('HTTP/1.1 301 Moved Permanently');
  header('Location: http://www.example.com/');
?>

Creating a database backed redirect system should be fairly easy. Create a script which looks at the $_SERVER[‘REQUEST_URI’] the requested path on the server looks in a “from_path” column in a “redirects” table in the database, and reroutes the visitor to a value in a “to_url” column. Then add this script as the 404-handler on your server and you’re almost set to go – the only thing missing is letting your script display the regular 404 page, if no “from_path” is found.