How to create Friendly URLs

What are Friendly URLs? Friendly URLs (sometimes know as tidy urls) are dynamic URLs (URLs with a query string attached) URLs that have been ‘re-written’ to appear as static pages — usually retaining important keywords and information in them. An example would be a product page that contained a list of product information retrieved from a database, with a search facility to enable you to see a product category in more detail. For example, the main page showing all products might be:

and when showing a product category, it might be:

A re-written Friendly URL for the above might look something like this:

So, how is it done? Well, there is more than one method to achieve Friendly URLs but the method I tend to use involves using .htaccess files. (note: this method will only work if you are using an Apache server with mod_rewrite enabled).

Create a text file (name it anything you like), open it and add the following lines:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^products-([^/\.]+)_([^/\.]+).html?$ products.php?category=$1&colour=$2 [L]

Once you have created the text file, upload it to the root level of your site and then change its name to “.htaccess”. (Note: htaccess files are invisible files, check first if there is one there already and if so, open it and copy & paste the text in to it).

The last thing you need to do is update any references to the URL in you html to use the new URL.

It’s pretty easy to pick up the basics, however – to make it even easier I have created a simple web app for creating .htaccess based Friendly URLs. Simply enter your exsisting full URL with query string, then make the appropriate choices to create your new URL and finally download your .htaccess file, ready to upload to your server.