Continued from page 1
RewriteRule ^(.*).html index.php?task=$1
What does all that mean? This is a rewrite rule which allows you to make your URLs looking as "static". In this example categories.html will be "translated" to index.php?task=categories. So you no longer need dynamic URL to list ther categories, but can write categories.html
But what do all these strange characters mean? - ^ character marks beginning. I.e. you tell server that it should not expect anything before it. - (.*) - This combination is most often used and it means literally "everything". So everything you type before ".html" (i.e. your fake file name) will be passed as: - $1 - This is a parametter, saying where first mask should be put. If you have more than one masks (masks are everything which you use to represent dynamich text or file names) you can use $2, $3 etc. You'll seemor ein following examples.
So, if you have categories.html it will be translated info index.php?task=categories, services.html into index.php?task=services etc...
What if you have more than one parametter? First, you should use some characters as delimiter:
RewriteRule ^(.*)-(.*).html index.php?task=$1&language=$2
Here how you can also pass task and language. For example: categories-englist.html will be translated into index.php?task=categories&language=english.
IMPORTANT: If you first write RewriteRule ^(.*).html index.php?task=$1 The second one may not work. You need to always start from most complicated rule to simplest one.
Make it Better: The rule (.*) is too general and often may prevent you of making more complicated rewriting rules. So it is recommended that you "limit" rules into something more concrete. Here are a couple of advices:
- Use "OR" operator. In our e-shop example we have only few possible "tasks" passed to index.php. Lets say: index.php?task=categories index.php?task=category index.php?task=product index.php?task=services
What will happen if you want to use your static file about.html? It will be rewritten into index.php?task=about and won't work. So you can use OR operator and limit rewriting only to cases you need:
RewriteRule ^(categories|category|product|services).html index.php?task=$1
This tells server to rewrite only if file name is categories.html OR category.html OR product.html OR services.html
- Using "numbers". You can easy limit rewriter to rewrite if it meets only numbers at a certain place:
RewriteRule ^category-([0-9]*).html index.php?task=category&id=$1
With ([0-9]*) mask you tell rewrite engine that on mask place it should expect onlly numbers. So if it see category-english.html it won't rewrite to index.php?task=category&id=english, but to index.php?task=category&language=english (because of rule we have shown above - RewriteRule ^(.*)-(.*).html index.php?task=$1&language=$2.).
Complete example: Here is how will look final .htaccess file for our imaginary e-shop:
-------- Options +FollowSymLinks RewriteEngine on
RewriteRule ^(.*)-(.*).html index.php?task=$1&language=$2. RewriteRule ^(categories|category|product|services).html index.php?task=$1 RewriteRule ^category-([0-9]*).html index.php?task=category&id=$1
The author is Senior Deveoper in PIM Team Bulgaria and Consultant in SEO PIM Team