Call (877) SITE-OPT (748-3678)

Code: mod_rewrite example

Chapter 9 - Advanced Web Performance Optimization

Essentially, mod_rewrite works as a smart abbreviation expander. For example, to expand r/pg into /programming Apache requires two directives: one turns on the rewriting machine (RewriteEngine On) and the other specifies the rewrite pattern matching rule (RewriteRule). The RewriteRule syntax looks like this:

RewriteRule <pattern> <rewrite as>

The preceding code snippet becomes:

RewriteEngine On
RewriteRule ^/r/pg(.*) /programming$1

This regular expression matches a URI beginning with /r/pg following immediately afterward. The pattern (.*) matches one or more characters after the pg. So, when a request comes in for the URI <a href="/r/pg/java/">Programming Java</a>, the rewrite rule expands this abbreviated URI into <a href="/programming/java/">Programming Java</a>.

Note that you can also use mod_rewrite in the same manner to map search-friendly URIs to database queries:

/keyword1+keyword2 /index?cat=153
Copyright © 2002-2017 Website Optimization, LLC. All Rights Reserved - Free website speed test - Privacy Policy
Last modified: February 19, 2009.

Follow us on: Twitter, Google+, Facebook, Linked In