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