A blog about digital & online

A little insight into rewriting URLs with PHP and Apache

I have recently lost my “URL rewriting virginity”, in that I have set up rewriting for the first time, using apache’s .htaccess file. For those that don’t know, .htaccess is a file kept in the root folder of the site that it will apply to. The file contains rules that affect the behaviour of your website.

It took me browsing several forums before I managed to learn how to rewrite both static URLs and URLs with dynamic parameter strings and values.

The apache module I found is called MOD Rewrite, and can be called from within a .htacces file.

A quick definition of URL rewriting in the capacity that I have required its use:

I use the GET keyword in PHP to store parameters provided in my URL to variables that I will then use throughout my PHP document. Doing this really helps me, because if I have multiple items of content that I want to pull out of a MYSQL database into a web page template, I can do so by providing MYSQL with the name or ID number of that content item.

Now that I have parameter values in my URL, it tends to look rather longwinded and technical such as: http://www.mysite.com/index.php?id=1&category=1&title=1

I would like to change this hideous & cumbersome URL to something a bit neater. In SEO, some search engines discard everything after the question mark, therefore not bothering to put your page in their index.

My URL, once rewritten will read something like: http://www.mysite.com/home/1/category/title. How do I go about this? Well I start my .htacces file:

RewriteEngine on

RewriteRule ^home/ index.php [NC]

In this example, anyone who types http://www.mysite.com/home/ into their address bar will end up seeing the output of my index.php document. That’s perfect, exactly what I wanted. Now if I want to add a dynamic, changing parameter such as the id of the content item, I add this line to .htaccess:

RewriteRule ^home/id/([^_]+) index.php?id=$1 [NC]

This will now replace ?id= with home/id/, and the variable id (22 for example) will go after, as in home/id/22.

To add a page title and category to this, I will write the rule as follows:

RewriteRule ^home/id/([^_]+) /([^_]+)/([^_]+)  index.php?id=$1&category=$2&title=$3 [NC]

Well, I hope you managed to make this work on your server, but if you had trouble, you can read more detail about using MOD Rewrite here:  .htaccess tricks and tips.. part two: url rewriting with mod rewrite.

There is one little issue I discovered when rewriting URLS in this way, which I haven’t managed to overcome easily. The issue is that when I rewrite a filename to read as a directory name (with a trailing slash after it), my browser now treats the current open URL as being in that directory. This is fine, except for a slightly irritating side affect: every call to a relative URL I have made on this page, now becomes relative to the directory that the browser thinks I am in. As a result of this, I have had to make all my relative links & anchors, relative image sources, relative PHP includes and relative CCS & JavaScript file references comply with the url structure I have created inadvertently with URL rewriting.

I found this unexpected surprise quite annoying, but it certainly hasn’t put me off rewriting my URLs, which just looks so much better to users, and my keyword rich post titles get seen in the URL by search engines! I will simply plan my URL structures accordingly in future.

Good luck!

About the Author

Phil Smulian
Phil Smulian likes to get his hands dirty when it comes to solving code problems. He also really loves making user experiences better than they were when he found them. Find him on Twitter @Bowsie

Leave a reply

Your email address will not be published. Required fields are marked *