A short article in human language
This article is not a complete quide to Apache's mod_rewtite neither to .htaccess. Its purpose is to help you - webmaster - to create "mod_rewriten" versions of your dynamic webpages even if you have limited technical knowledge. I won't show you all tips-and-tricks - my aim is to bring all complexity of Apache's documentation to 1-2 pages of human lanuage - easy and fast.
What is mod_rewrie?
Mod_rewrite is Apache extension which allows you to "rewrite" URLs of your web pages. If your server supports this technology (most linux webhosts do nowadays) you are able to rewrite virtually any URL into anything you like. Most often it is used to rewrite URLs of dynamicly generated webpages such as www.mywebsite.com/index.php?par1=1&par2=2&par3=2... This can easy be 'translated' into www.mywebsite.com/par1/par2/par3
- Search engine optimization - there are a lot of debates on this topic, but it is still true that static-looking links rank better than dynamic ones. Here is a comfirmation from Google on that topic:
"Your pages are dynamically generated. We're able to index dynamically generated pages. However, because our web crawler could overwhelm and crash sites that serve dynamic content, we limit number of dynamic pages we index. In addition, our crawlers may suspect that a URL with many dynamic parameters might be same page as another URL with different parameters. For that reason, we recommend using fewer parameters if possible. Typically, URLs with 1-2 parameters are more easily crawlable than those with many parameters."
- User-friendlyness - Some users remember URLs visally. Even if they bookmark, they can easier recognize a link like www.mywebsite.com/services.html than www.mywebsite.com/index.php?task=12 for example.
- Security - mod_rewrite helps you hide parametters passed in application. Basicly your dynamic pages should be secure enough even without mod_rewrite. But hiding parametters will decrease danger of attack
How to use it? Mod_rewrite is really powerful if you are familiar with regular expressions which it uses. But learning whole pattern syntax can be quite complicated, especially for non-technical user. Thats why i'll teach you at several simple patterns which are pretty enough to get your website URLs rewritten.
Lets start: First you need to create a file called .htaccess and place it exactly in folder where you want rewriting to take effect (it will also take effect over all subfolders). In case you already have a .htaccess file you can simply add lines to it (if it already has mode_rewrite directives you can mess them however). Open it in a simple text editor an start with:
Options +FollowSymLinks RewriteEngine on
Now rewrite engine is switched on. You can now start adding as many rewrite rules as you want. The format is simple:
RewriteRule rewrite_from rewrite_to
Here "RewriteRule" is static text, i.e. you should not change. "rewrite_from" is address which will be typed in browser and "rewrite_to" - which page server will actually activate. Both of these can contain "masks", but in "rewrite_to" we will only use $ and will discuss more or "rewrite_from" part. Let me "meet you" with very few masks you'll need and bring you some samples. You'll see how easy is it.
Let's stop talking theory and see an example. Let's imagine your server runs an e-shop, which uses URLs like index.php?task=categories to list categories, index.php?task=category&id=5 to show a category contents and other parametters in 'task' to do other things.