In this tutorial you will learn to use Apache's mod_rewrite to create search engine friendly urls by making dynamic urls appear as if they were static.
Dynamic URls and Search Engines ?
Dynamic sites much more popular now then they were a few years a go. Pages for these site are generated using variables sent to a backend script through the URL. A typical url of dynamic site may look something like this
http://www.sitename.com/index.php?category=11&subcat=ServersideThe trouble is that while search engines such as google tend to give sites with urls like this a lower ranking in search engine rankings, and these type of urls are also bad from a usability and security prespective.
A solution to this is possible using mod_rewrite to rewirte these urls to something more SE friendly.
A basic url rewrite
Before starting you need to make sure that your site is hosted on an Apache server and that mod_rewrite is installed and enabled.
Suppose you want the document located at this long url
http://www.tutorio.com/lsdjf/klsjdf/sdlkfj/thisisreally-long/and/frustrating.phpwith a url that looks like
http://www.tutorio.com/shortandsweet.htmlCreate an .htaccess file and place the following code
Options +FollowSymLinks RewriteEngine On RewriteRule ^shortandsweet.html lsdjf/klsjdf/sdlkfj/thisisreally-long/and/frustrating.php
The contents of the long url can now also be accessed via the short one. However keep in mind this is NOT a redirection, in that you are not transferred to the long url when you type in the short one, or vice versa.
Make Dynamic Pages appear Static
Going back to the first example url
http://www.sitename.com/index.php?category=11&subcat=ServersideHow do you make this url appear as if it was static ?
Options +FollowSymLinks RewriteEngine On RewriteRule ^category-([0-9]+)/([0-9A-Za-z]+).html index.php?category=$1&subcat=$2
Explanation
The category number and the subcategory name are variables, these are now represented by $1 and $2. The round brackets and the stuff inside them will be replaced by the variables. The things inside the round brackets are the regex rules for what the variable can contain. Example [0-9] means that it can contain any number from 0 to 9 and the + sign means that the number can be repeated 1 or more times. You will now be able to access the url
http://www.sitename.com/index.php?category=11&subcat=Serversidefrom
http://www.sitename.com/category-11/Serverside.html