Apache Tutorials
Configuring OfBiz To Use A MySQL Database
Installing Apache OfBiz
Learning to use mod_rewrite

Learning to use mod_rewrite

Introduction to mod_rewrite

Apaches mod_rewrite is the packages greatest and most horrifying module. Mod_rewrite gives you the ability to manipulate URLs based on almost any condition imaginable. Server variables, environment variables, HTTP headers, and time stamps can be used to match rule sets based on regular expressions. Rules can be stored in config the config file or .htaccess files.

Now before we jump right in and start making rules remember you won't learn all of mod_rewrite today.


RewriteEngine On
RewriteBase /
RewriteRule ^/source/$ destination.html

The first thing you need to do is enable rewriting by doing RewriteEngine On. RewriteBase is used to set the base directory for the rule. The base directory is stripped off the URL prior to processing rules and added back on after. Finally the rule that gets something done RewriteRule is used to declare the URL based rules.

Mod_rewrite expects URL condition to match then the substitution. Optionally and not shown yet you can add flags to the end of the rule. We'll get to that shortly. In this RewriteRule we redirected the directory source to destination.html. ^ is the regular expression meta-character for start of subject and $ represents end of subject.

Practical uses of mod_rewrite

Now lets put a little more usefulness into our rule.


RewriteRule ^/tutorials/(.*)/$ /tutorials.php?ID=$1

This RewriteRule maps the easy to type URL /tutorials/1324/ to the bulky tutorials.php?ID=1324.

 1 2  >> Flexibility of mod_rewrite
New Content

Related Information
No content found