EASY Search Engine Friendly URLs with mod_rewrite
One key to getting good rankings on SERPs (Search Engine Results Page) is using mod_rewrite to make search engine friendly urls:
Example: http://www.mysite.com/keyword/this-is-loads-of-great-keywords
A novice web developer would look at the above address and think that this meant creating a directory called 'keyword' with a sub-directory of 'this-is-loads-of-great-keywords'. This novice web developer would be wrong. Using the mod_rewrite feature is required to creating urls like this, with not actually creating the page itself. Confused? So was I when I first started trying to do this. There are no easy to follow tutorials on the web which explain this feature well enough or in-depth enough to be useful. So, here is my attempt at an easy to understand yet in-depth tutorial on using mod_rewrite to create search engine friendly urls.
This tutorial is making the assumption that you are on an Apache web server which has the mod_rewrite feature enabled.
Firstly, we need to create a .htaccess file. Relatively speaking, this file tells the browser where to go when a url is typed in. To enable mod_rewrite in a directory, create the file .htaccess in a text editor and put the following at the top of the file:
RewriteEngine On
Now, this is where the fun begins. In the example I'm going to be using in this tutorial, I will have a file called article.php which needs an GET variable passed to it called article_id (ex: article.php?article_id=89). I'm going to change this into a much nicer url of '/any-text-here-89'. So to do this I enter the following line into my .htaccess file:
RewriteRule ^([a-zA-Z0-9_-]+)-([0-9]+)$ article.php?article_id=$2 [L]
WHOA! What is all this strange syntax here? Well, its called regex (regular expression). I'm not a fan of regular expressions, its like a secondary coding language for string formatting, but it's extremely useful for this. So let me explain the above line.
We start with 'RewriteRule'. This tells the server that we want to declare a rule for a url to be rewritten if it matches the following pattern.
Next we have the ^ character. This is extremely important and must be the first thing after RewriteRule. I believe it means 'start from the end of the domain' to match the pattern.
Now we have ([a-zA-Z0-9_-]+). This is regex for 'allow any number of characters and numbers with the addition of the underscore (_) and the hyphen (-). The + on the end tells it 'any number of characters'. So 'this-is-a-string' or 'this-is-a-really-long-string' would match.
The next part is a a hyphen (-). This means it must have a hyphen after the series of characters and numbers.
After that, we have ([0-9]+). This is regex for 'allow any number of numbers'.
Then at the end we have $. This means, 'and nothing more'. It may not be apparent how important this individual character is, but when you start adding multiple RewriteRules that start becoming very similar, if you don't have this character, you will have some crazy problems if you don't put your RewriteRules in an organized order.
So, then we have a few spaces, followed by article.php?article_id=$2. Now, this is more like it. Something that resembles something in English. This tells the browser where to send the matched pattern to. So anything that matches the regex will go to article.php and pass something as aritcle_id.
The $2 tells the browser to use the second variable from the regex pattern. Each regex variable is grouped by the parentheses (()). So in this example, the second variable will be what matches ([0-9]+). So if the url ends up being 'http://www.mysite.com/this-is-an-article-89', the second variable is '89'.
It is also very important to add '[L]' at the end of each RewriteRule line. This will tell the browser to stop trying to match the url to any other RewriteRules. Not adding this can cause great problems with pages going to the wrong places.
With this in mind you could create a more complex RewriteRule, which passes more than 1 variable. Here is an example:
RewriteRule ^category-([0-9]+)/article-([0-9]+)$ article.php?category_id=$1&article_id=$2 [L]
The above example passes 2 variables to article.php. So, if I went to http://www.mysite.com/category-9/article-448, the above rule will pass a category_id of 9 and an article_id 448 to article.php.
Hopefully this tutorial has helped you understand mod_rewrite better. I suggest planning out your url structure before writing out these RewriteRules to avoid any problems with rules that might have duplicate matches. For reference, here are some helpers to get you on the right track with your RewriteRules:
RewriteRule ^(.*) - Matches anything after the domain (ie: www.mysite.com/php or /php.php)
RewriteRule ^(.*).html - Matches anything with a .html at the end (ie: www.mysite.com/php.html)
([0-9]+) - Matches must contain any number of numbers
([a-zA-Z0-9_-]+) - Matches any number of alphanumeric characters
