URL Rewriting with mod_rewrite

mod_rewrite matches the requested path against regular expressions, checks conditions, and then maps the request to another resource or redirects the browser. Ralf S. Engelschall wrote it in 1996, and the Apache 2.0 129 rewriting guide opened with Brian Moore's verdict: "mod_rewrite is voodoo. Damned cool voodoo, but still voodoo." Redirects and Pretty URLs to Complete .htaccess Recipes are built from its pieces.

In .htaccess it needs AllowOverride FileInfo (AllowOverride), or the file fails with a 500 and RewriteEngine not allowed here, and Options FollowSymLinks or SymLinksIfOwnerMatch, or every rule answers 403 with AH00670. The examples run on one site, after sudo a2enmod rewrite headers:

/etc/apache2/sites-available/example.conf, the test site for Section 2.5XML
<VirtualHost *:8105>
  ServerName example.com
  DocumentRoot /var/www/example
  <Directory /var/www/example>
    Options FollowSymLinks
    AllowOverride FileInfo
    Require all granted
  </Directory>
  LogLevel warn rewrite:trace3
</VirtualHost>

show.php prints the $_GET it receives as JSON after get:. The helper t prints each URL's status, any redirect target, and that JSON:

The test helper used throughout this sectionApache config
alias curl="curl -H 'Host: example.com'"     # the site sees its production host name
t() { for u; do
  r=$(curl -s -o /tmp/body -w '%{http_code} %{redirect_url}' "localhost:8105$u")
  printf '%-21s %s %s\n' "$u" "$r" "$(sed -n 's/^get: *//p' /tmp/body)"; done; }

Subsections