Search Engine Friendly URLs with mod_rewrite

Tidy up your URL by using mod_rewrite. Not only will this hide the query string, but it will make the URL look more presentable.

Friendly URL’s are a very important thing to have when developing a dynamic content site that uses long query strings. A query string is something similar to a URL like you’ll see on Ebay, Google and other big sites. If you’re running a small to medium size website and you want to make your URL’s a little easier to find you can do so via Apache’s mod_rewrite by following this tutorial.

Prerequisites

You’ll need to have Apache’s mod_rewrite compiled into your Apache Web Server and you’ll need to have access to setting some options up in your httpd.conf or at least have a server admin who’s willing to do it for you. You can follow my tutorial for installing Apache web server on Linux here. For quick reference, the configure options for mod_rewrite are:

./configure
–enable-module=rewrite
–enable-shared=rewrite

I strongly urge you to carefully consider what other options you need to compile into your Apache installation. The above example is only for this one module.

Once you’ve compiled Apache to use mod_rewrite, you will need to setup the httpd.conf file. Here’s how I set mine up for this particular situtation.

<Directory /www/htdocs/yoursite>
Options ExecCGI FollowSymLinks Includes MultiViews
</Directory>

Some of the above directory options may not be necessary for what you are trying to do. You’ll have to decide if you need them. If you are having problems you can also try to use AllowOverride All to see if you can get it to work.

Planning

It’s a very important idea that you plan how to rewrite you URL’s because you don’t want to go back and change the links over and over again. Once you’ve got a good steady map of how to rewrite your links, you can go ahead and modify a test script to see if everything worked. If your links work properly then you’ll be able to move on and modify all existing links. Let’s take a look at this example below:

Query String URL: http://yoursite.com/tutorials.php?req=tutorial&id=3&page=0

mod_rewrite URL: http://yoursite.com/tutorials/3/0.php

Doesn’t that look much friendlier?

Once you’ve compiled Apache, edited the httpd.conf file and planned your URL rewriting, you’re ready to setup your .htaccess file which maps out what to do with each URL. Here’s an example of what to put in your .htaccess file. I’ll explain how this works below.

RewriteEngine On

RewriteRule ^tutorials/(.*)/(.*).php /tutorials.php?req=tutorial&tut_id=$1&page=$2

In the example above we have two lines. The first line RewriteEngine On starts the mod_rewrite engine. The second line is what does all the work.

RewriteRule ^tutorials/(.*)/(.*).php /tutorials.php?req=tutorial&tut_id=$1&page=$2

Here you are starting a RewriteRule

RewriteRule ^tutorials/(.*)/(.*).php /tutorials.php?req=tutorial&tut_id=$1&page=$2

This is the start of a regex that allows you to create wildcards for the URL that your friendly URL. This allows us to use whatever we want between the (.*) and compare them to the second part of this rewrite rule.

RewriteRule ^tutorials/(.*)/(.*).php /tutorials.php?req=tutorial&tut_id=$1&page=$2

This is the final part of the rewrite rule that tells us how to map the friendly url in part 2 of the RewriteRule to the actual URL that our script was written for. mod_rewrite will translate part one to part 2 automatically.

This works by using regex. The first (.*) will be $1 and the second (.*) becomes $2 in the translation. You can do this as many times as you want for your URL.

Say for example we had this url: tutorials/13/0.php.

We’ll be matching up /tutprials.php?req=tutorial&tut_id=13&page=0 by using mod_rewrite!

Believe it or not that’s all that’s really to mod_rewriting!

You can really get carried away with this by adding category titles to some of your rewrite URLs to make them even more search engine friendly. All you have to do is create another regex and don’t use that variable in your translation.

Rewriting your URLs can be valuable. We took the time to do that for most of our URLs on this website and it makes things simpler for your visitors. I encourage you to try this out and see if you like the results. Also I would like to add that you can do this by exploding your URL’s in php and you would not need Apache’s mod_rewrite. A tutorial will come for this someday, but for now you can get started using this “Voodoo” tool for Apache Web Server.

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan