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

Learning to use mod_rewrite

Some flexibility of mod_rewrite

Now lets look at some more of the flexibility of mod_rewrite, one or more RewriteCond can be used to set conditions in which to execute rules. RewriteCond can set conditions based on server variables, enviroment variables, HTTP headers, and time stamps.


RewriteCond %{HTTP_USER_AGENT} !^my-application v2.02$
RewriteRule * /upgrade.html

Here we used the HTTP_USER_AGENT HTTP header variable to restrict access to our application to only the latest version and send all others to upgrade.html. This can also be used as an added layer of security although you shouldn't base your security around this as its really a minor annoyance. Another common use for this is preventing people linking directly to your images, and can be done as follows.


RewriteCond %{HTTP_REFERER} !^(http|https)://domain.com/* [NC]
RewriteRule /images/* /image-blocked.jpg

If your company/organization owns multiple domains that you wish to display a mirror mod_rewrite can handle that. The [NC] at the end of the RewriteCond is a flag designating case insensitivity. In the following rewrite we take all requests going into minordomain.com and remap them to maindomain.com.


RewriteCond %{HTTP_HOST} ^http://minordomain.com/* [NC]
RewriteRule ^/(.*) http://maindomain.com/$1 [R=301,L]

The [R=301,L] are flags for the RewriteRule. The R flag tells mod_rewrite to redirect the traffic with the corresponding status code. The 301 redirect tells the client retrieving the page that the content has permanently moved and the L stands for the last rule to process. Other common flags for RewriteRule are F -- forbidden, T -- force MIME type, C -- chain with next rule, and N -- next round which restarts processing rules from the beginning with the URL in its current state of processing.

Introduction to mod_rewrite <<  1 2
New Content

Related Information
No content found