Wednesday, September 01, 2010

Search Engine Friendly URLs with WAMP & Apache

It has been a very hot topic for a long time. The Friendly URLs have taken the Internet by storm, mostly because the expanding use of Ruby on Fai..Rails. :) Today we are going to look at how we can create friendly URLs using the Apache HTTP server found in the WAMP software installation. It will actually be very simple! Depending on your environment and knowledge we are first looking at how we can add the support to use friendly URLs in WAMP and a quick look on how we can do it without the WAMP software. If you want to use the WAMP software you can download it below:
  • Download the WAMP software

We are going to start looking at the WAMP software first. After you have downloaded and installed the WAMP software you should find a short cut item on your Desktop. Click on that, in the right corner of your screen you should see some sort speed limit panel turning red, yellow and lastly white. If it doesn’t turn white something is wrong with the setup. If you haven’t changed anything from the default settings when installing the WAMP software. The activate the module making it possible to create friendly URLs we simple do this:
  1. Left click on the WAMP icon in the right corner of the screen.
  2. Move you mouse over the ‘Apache’ link.
  3. In the popup menu, move your mouse over the ‘Apache Modules’ link.
  4. Move down in the list by pressing the down arrow image until you find rewrite_module.
  5. Click on the rewrite_module.
  6. The WAMP software should restart, if it isn’t do it your self be left clicking on the icon again and choose ‘Restart All Services’.
So now we have the rewrite_module activated in WAMP. To active the rewrite_module without WAMP you need to go to your Apache directory, or make a search with the keyword httpd.conf. That file contains a lot of config options for your Apache HTTP server. Inside that file, search for:
LoadModule rewrite_module modules/mod_rewrite.so
That module will have a # in front of it, remove that and save the httpd.conf file and restart your Apache server. This is how you activate the rewrite_module in Apache and not via the WAMP software. Seems rather easy, right?
Okay, so what we have done so far is to activate a module we are going to use when we want to write friendly URLs. Friendly URLs you say? What is that? In my 12 SEO Tips post I talked about friendly URLs and how they are much better for web spiders, visitors and for yourself. The result after turning your unfriendly URLs into friendly once are this:
We turn this dynamically created, unfriendly URLs:
http://www.yourdomain.com/users.php?id=12
Into this friendly URL:
http://www.yourdomain.com/users/12/
Does that look a lot better than the first one? Especially if your website is targeting people with a lack of Internet experience. The simpler and cleaner your URL look like, the better. We will even have the chance of enter a name.html and turn it into name.php without the visitor knowing anything about it. Why should we like to  do that? Once again, it’s better for the web spider and in the end your chance of being found in a search will expand.
So, the rewrite module activated and running, what do to next? We are going to create a file with the name .htaccess. This file is a configuration file working tightly with the Apache HTTP server.  This is how the Apache team describes it:
.htaccess files (or “distributed configuration files”) provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.
Inside this file we are able to create patterns which will match the incoming URL. If the URL match any of our patterns we are able to transform the incoming URL into something else. What does this mean? It means that you don’t have to change any PHP query strings code to transform your URLs. With the .htaccess file you are able to redirect your visitors to the correct page you want them to look at, but the address looks the same to them. Here is an example:
Your visitor writes:
http://www.yourdomain.com/users/12/
In your .htaccess file you do this with the incoming URL:
http://www.yourdomain.com/users.php?id=12
Looks like magic? It is! Naa, not really but it looks a lot nicer and, as I have told you several times now, it helps your search rankings.
The following will be an example of a .htaccess file. We will go through it line by line and see what it does.
1. RewriteEngine on
2. RewriteRule ^(/)?$ /index.php [L]
3. RewriteRule ^([^\/\.]+)\.html$ $1.php [L]
4. RewriteRule ^([a-z]+)/([0-9]+)$ /$1/$2/ [R]
5. RewriteRule ^([a-z]+)/([0-9]+)/$ /$1.php?id=$2
Line 1: Activates the rewrite module for this particular folder. This line is required for ALL .htaccess files.
Line 2: We rewrite http://www.yourdomain.com/ to http://www.yourdomain.com/index.php. The [L] at the end is telling the .htaccess file to stop rewriting if this pattern has been match.
Line 3: We rewrite all incoming .html files into .php. We do this because it’s easier for visitors to enter .html instead of .php.
Line 4. We rewrite http://www.yourdomain.com/users into http://www.yourdomain.com/users/. We just add the extra / at the end to say: “Hey, this is how we like it”. The [R] at the end is telling the .htaccess to continue the rewrite regardless if the pattern matched or not.
Line 5. Is rewriting http://www.yourdomain.com/users/12 into http://www.yourdomain.com/users.php?id=12.
Very simple. The pattern is using the cross-language script regexp, or regular expressions.  A good place to start looking at regexp if you haven’t heard about it before is the link below:

So by using the rewrite module found in the Apache HTTP server and creating a .htaccess file inside our main directory we can accomplish the friendly URLs task. Excited? I know I was the first time. Start right away!

No comments: