2

How to create Pretty URLs?

Monie on December 13th, 2009 in Tutorials
htaccess

Learn this fun technique on how to make your existing link becomes more pretty and SEO friendly.

My programming world has been recently updated and upgraded with a new programming technique called htaccess. I know it’s a bit late for me considering that this programming has been out there ever since the apache server itself, but anyway, late is better than never, am I right? So here I am talking to you about how to create a pretty url from your existing websites.

What is Apache mod_rewrite module?

The Apache server’s mod_rewrite module gives you the ability to transparently redirect one URL to another, without the user’s knowledge. This opens up all sorts of possibilities, from simply redirecting old URLs to new addresses, to cleaning up the ‘ugly’ URLs – giving you URLs that are friendlier to both readers and search engines.

The Apache module mod_rewrite is a killer one, i.e. it is a really sophisticated module which provides a powerful way to do URL manipulations. With it you can do nearly all types of URL manipulations you ever dreamed about. The price you have to pay is to accept complexity, because mod_rewrite’s major drawback is that it is not easy to understand and use for the beginner. And even Apache experts sometimes discover new aspects where mod_rewrite can help.

In other words: With mod_rewrite you either shoot yourself in the foot the first time and never use it again or love it for the rest of your life because of its power. This paper tries to give you a few initial success events to avoid the first case by presenting already invented solutions to you.

Case Study – Example

Consider the following address:


http://www.mysticborneo.com/index.php

http://www.mysticborneo.com/sabah.php?mb_category=Nature+Excursions+Packages

Taken from my latest project, Mystic Borneo Travels and Tours, as you can see, this is what we called an ugly url, but it is becoming increasingly common in these days of dynamically-generated pages. The problem with this kind or url is that it exposes the underlying technology of the website (in this case PHP).

This can give potential hackers clues as to what type of data they should send along with the query string to perform a ‘front-door’ attack on the site. Information like this shouldn’t be given away if you can help it.

Luckily, using mod_rewriting, we can clean up this URL to something far more manageable and of course more cute and pretty. For example, we could map it to:


http://www.mysticborneo.com/home

http://www.mysticborneo.com/sabah_packages-Nature Excursion Packages

Much better. This URL is more logical, readable and memorable, and will be picked up by all search engines.

Lets try out the link:

Let’s Start Coding

To start, considering that you guys knows and have experiences developing website before and have some basic knowledge in php programming, navigate to the root directory of your website (www), there you will find a file named “.htaccess” (without the quotes).

Open the file in your favourite text editors (notepad will do just fine), and here is the code for the solution of the case study above.

RewriteEngine On
# Websites with the address "index.php" can be access just by typing "home"
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule home index.php
# Creating Pretty URLs
RewriteRule ^(.*)_packages-(.*)$ $1.php?mb_category=$2 [L]

Let’s go through each code line by line. The first line tells Apache that we are going to use Rewrite Engine (mod_rewrite). The next two lines are very, very important. It restricts rewriting URLs only to paths that do not actually exists. This prevents the rules below from matching example.com/images/.png for example. The first prevents existing directories with the !-d flag and the second with !-f means ignore existing files.

The next three lines are the actual URL rewriting commands. Each line creates a rule that tries to match a regular expressions pattern against the incoming URL.

  • The caret, ^, signifies the start of an URL, under the current directory. This directory is whatever directory the .htaccess file is in. You’ll start almost all matches with a caret.
  • The dollar sign, $, signifies the end of the string to be matched. You should add this in to stop your rules matching the first part of longer URLs.

So, this rule will make your server transparently redirect from /sabah.php?mb_category=Nature+Excursions+Packages to the /sabah_packages-Nature Excursion page. Your reader will have no idea that it happened, and it’s pretty much instantaneous.

There is plenty of other things that you could do with .htaccess file such as access restriction, custom error page, cache control and many more. I am enjoying them as we speak, I hope you do so.

2 Responses so far.

  1. Frank says:

    Great tips man… nicely explained!

  2. Monie says:

    I’m happy you liked them.. Thanks for reading them.

Leave a Reply