Main Content

How to set up short and meaningfull alternative URLs

Archive - Originally posted on "The Horse's Mouth" - 2010-10-02 11:54:51 - Graham Ellis

Wouldn't it be nice is URLs were shorter? Well - there are ways of making them shorter these days and you'll find them on networking sites like Twitter ... http://bit.ly/d7t8ID or http://bbc.in/XTQ8V . But where such URLs gain in brevity, they loose in comprehensibility.

But it's very easy to provide brief URLs that *are* meaningful too - and to do so on your own site if you have a short domain name (not www.whydidiregistersomethingthislong.co.uk) - we've done it, and let me show you how.

I wanted a quick link to our hotel booking page at https://lightning.he.net/~wellho/hotel/reservation.php - I wanted to go there from wellho.info/booking (yes, do try that link - it will work!).

Here's how I did it:

1. On the server for the domain that's got the short URL, I added the following to the .htaccess file in the DocumentRoot (the server's running Apache httpd with mod_rewrite and AllowOverrides On)

  RewriteEngine On
  RewriteRule ^([a-zA-Z0-9]+)$ http://www.wellho.net/short/ [L]


That redirects any URLs in the root directory that are just strings of letters and digits to a more structured directory at a longer URL within our main domain.

2. In the "short" directory on the main domain server, I've also got a rewrite line which sends the received name to a PHP script (requirements on this machine - httpd, mod_rewrite, Overrides allowed and PHP):

  RewriteRule ^([a-zA-Z0-9]+)$ divert.php?shrtfrm=&%{QUERY_STRING}

3. The PHP script looks up the code in $_REQUEST[shrtfrm] from a file or database, and sends out Location header telling the browser where to go:

  header("Location: $fn");

And it is nice! .... I have a whole lot of meaningful short links I can give to people, and I can use them as a switchboard too - so that if we come up with a new map there isn't a huge amount of extra work to do to tell people where it is.

Let's see if you can guess what you'll get if you follow the following links (which will open in a new window):

wellho.info/weather

wellho.info/map

wellho.info/course
wellho.info/coursebooking

wellho.info/hotel
wellho.info/hotelbooking

wellho.info/askus

wellho.info/coffeeshop
wellho.info/chamber
wellho.info/carnival


I'll confess to having done a bit more too .... If you put in a number, it will take you straight to that article number in "The Horse's Mouth", and if you put in a word that happens to be the name of a page on our wiki, it'll take you to a page there.

wellho.info/2202
wellho.info/2894
wellho.info/727

wellho.info/beanacre
wellho.info/coatewater
wellho.info/westwoods

wellho.info/phonein
wellho.info/walkin

And - yes - if you enter something that's not on our site you'll get an index of all the special links:

wellho.info/alpaca


The images used to illustrate this article all come from pages that are short-linked above. Can you find them?