|
|
MOD_REWRITE for DummiesA short article in human language This article is not a complete quide to Apache's mod_rewtite neither to .htaccess. What is mod_rewrie? Mod_rewrite is Apache extension which allows you to "rewrite" the URLs of your web pages. Why mod_rewrite? - Search engine optimization - there are a lot of debates on this topic, but it is still "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 the number of dynamic pages we index. In addition, our crawlers may suspect that a URL with many dynamic parameters might be the 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 the 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 the parametters passed in the application. Basicly your dynamic pages should be secure enough even without mod_rewrite. But hiding the parametters will decrease the danger of attack How to use it? Lets start: Options +FollowSymLinks Now the 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 the address which will be typed in the browser and "rewrite_to" - which page the 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 the 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 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. But what do all these strange characters mean? 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: IMPORTANT: If you first write RewriteRule ^(.*).html index.php?task=$1 The second one may not work. You need to always start from the most complicated rule to the simplest one. Make it Better: - Use the "OR" operator. In our e-shop example we have only few possible "tasks" passed to index.php. Lets say: index.php?task=categories 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 the OR operator and limit the rewriting only to the cases you need: RewriteRule ^(categories|category|product|services).html index.php?task=$1 This tells the server to rewrite only if the file name is categories.html OR category.html OR product.html OR services.html - Using "numbers". You can easy limit the 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 the rewrite engine that on the 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 Complete example: Here is how will look the final .htaccess file for our imaginary e-shop: Options +FollowSymLinks RewriteRule ^(.*)-(.*).html index.php?task=$1&language=$2. Article Tags: K=$1&am Languag Source: Free Articles from ArticlesFactory.com
ABOUT THE AUTHOR |
||||||||||||||||||||||||||||||||||||||||||
Partners
|