Ever wondered how WordPress sites get those nice search engine friendly URI’s? Whereby it changes http://www.example.co.za/?p=867 to something like http://www.example.co.za/contact-us? Well it’s actually very simple to achieve and can provide great benefits to your site, with regards to both users and search engines. To modify your URI’s you need access to the .htaccess file as well as making sure that the rewrite module has been enabled on Apache on your server.
I am going to give a few examples as I know not all websites are built the same.
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^/yellow-bicycles$ /product.php?product=13 </IfModule>
This will essentially point you to the complicated URI by typing in the easy to read URI. As you can see this is already proving to be very handy.
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^/bicycles/yellow$ /product.php?product=bicycle&colour=yellow RewriteRule ^/bicycles/red$ /product.php?product=bicycle&colour=red RewriteRule ^/bicycles/blue$ /product.php?product=bicycle&colour=blue </IfModule>
This now includes 2 variables and as you can see, makes things very simple and easy to read and understand.If for example you point your users to the following link:
what its actually doing is pointing the user to:
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^products/([^/]+)/([^/]+)$ product.php?product=$1&colour=$2 [NC] </IfModule>
This is where it gets REALLY useful. Here you can have one RewriteRule for numerous pages! It essentially takes the first variable and uses that for the product variable and the second for the colour. This can obviously be extended to suite your requirements. The [NC] at the end of the RewriteRule makes that rule case sensitive.
There are many more uses with the mod_rewrite module. I have just covered the basics here. Go out and give it a bash! Next time we’ll dive into Rewrite Conditions.