Wednesday, 29 November 2006

Using Apache's rewrite module to aid Search Engine Optimization

Just recently I have been dabbling with the effects of various Search Engine Optimisation (SEO) techniques and thought I would report on one that I have found particularily effective. I will also attempt to keep this post focussed on the theory, and as such will not contain much related technical information.

The Apache Web Server contains a very useful module called Mod Rewrite, and is one method I use to help the visibility of my sites within Search Engines and I will be using one of my sites as a live example of how this method works.

One of my sites is a Pro Evolution Soccer League and Community site that I created over a year ago. Since then it has had many overhauls, the most recent changes have included the use of the Apache rewrite module.

The site was developed with a PHP back end meaning that a lot of pages had nasty looking URLs, an example of this is the league pages:

/leagues.php?viewLeagueId=546

I have since updated them, with the help of Apache Mod Rewrite, to look like this:
/Leagues/Euro-Leagues/Euro-League-3/546/

Now not only does the new URL look nicer to any user, it also looks good to Search Engines. To prove how this works check out the following google search and you will notice that my site [Backofthenet] is ranked 3rd or 4th from the top for "Euro League".

If you look at the google entry for it you will see it highlights the search terms you used in the URL, approximately half of the URL is in bold meaning it ranked highly for that search. You should try do this for anything you want people to be able to search for.

The general aim is the same for all sites, whether you create static files or use mod rewrite to achieve the same thing, your URLs should always contain keywords related to the page.

Mod rewrite uses regular expressions to invoke rules that you create. You write a condition [using regular expressions] and then a rule. It's similar to and if block, where you only trigger the code inside the if block, if the test passes. I won't get into regular expressions, but here is an example of how your re-write rule should be written:

#www.mysite.com/profilename
RewriteRule ^([A-Za-z0-9-]+)$ /profile.php?viewProfile=$1


The line beginning with # is a comment line, if you have a number of rules you should comment them.
The second line catches all URL requests where letters, dashes and numbers follow the domain like the comment suggests, it then passes that to profile.php as an argument.

You will also notice I use hyphens [dashes] instead of spaces in the URL. If you use spaces in your URLs then any browser viewing the link will encode it into a hexadecimal reprasentationturning any space into %20. This does not look good, and some Search Engines cannot index such links, or at best prefer not to. Most Search Engines interpret a hyphen as a space, so by replacing all spaces in your URL with hyphens you can seperate your keywords without any of the drawbacks of using a space.

To replace spaces with hyphens in your URL you can simply use the following code:
str_replace(" ", "-", $string);

As mentioned at the start there isn't a great deal of technical help here, but here are some links in case you are having problems with the mod rewrite and str_replace.

0 comments: