Apache’s Mod Rewrite

Module mod_rewrite (Apache’s URL Rewriting Engine) is such a dandy feature and not just for the oft touted “friendly URLs”, which are admittedly cool, but it really shines as an alternative means of managing moved files, in lieu of a gazillion redirection files.

Sadly, when, in the low days of last Winter, I set out to reduce Pizza’s 404’s to as near to zero as possible I took what I thought was the smart and easy route. Checking the server logs for 404s I created appropriate redirection files for each missing HTML page. This works, and if you’re unable to use rewrite by all means get rid of those 404’s any way you’re able, but using meta refresh tags accomplishes the task in a far too obvious way; the user sees your “we’ve moved” page, if only for a few seconds before the page refreshes and loads the new resource.

Mod rewrite masks all of this, and leaves an appropriate “forwarding address” for search engines.

Apache httpd.conf

The place to begin is in Apache’s httpd.conf file, you need to locate the two places that will allow directory level controls for rules such as this. OK, is this the only way, well, no. But this is my story, of how I slowly did this, so this is a way that works.

  1. <Directory />
  2.   Options FollowSymLinks
  3.   AllowOverride All
  4. </Directory>

Be sure to find both occurrences of AllowOverride. I wasted an hour attempting to resolve why my mod rewrite in .htaccess wasn’t working (you’ll know if it isn’t working when all you get are 404s) before noticing the second place where AllowOverride needed to be set.

AllowOverride All

.htaccess

OK, having told Apache to look for and execute .htaccess files for each directory you’ll need to write ’em. The r=301 will include redirection message in the http header, making search engines happy. The nc is non-casesensitive.

  1. <IfModule mod_rewrite.c>
  2. RewriteEngine On
  3. RewriteBase /
  4. RewriteCond %{REQUEST_FILENAME} -f [OR]
  5. RewriteCond %{REQUEST_FILENAME} -d
  6. RewriteRule ^.*$ – [S=100]
  7. RewriteRule ^bmanphotos\.htm$   /photos/burningman.htm  [r=301,nc]
  8. RewriteRule ^halloween02\.htm$  /photos/halloween02.htm [r=301,nc]
  9. RewriteRule ^ksphotos\.htm$     /photos/kansas.htm      [r=301,nc]
  10. RewriteRule ^ludders\.htm$      /photos/ludders.htm     [r=301,nc]
  11. RewriteRule ^picture\.htm$      /photos/index.htm       [r=301,nc]
  12. RewriteRule ^swfeen04\.htm$     /photos/sfween04.htm    [r=301,nc]
  13. RewriteRule ^superfast\.htm$    /photos/superfast.htm   [r=301,nc]
  14. RewriteRule ^thumbs\.htm$       /photos/index.htm       [r=301,nc]
  15. RewriteRule ^z\.htm$            /photos/z.htm           [r=301,nc]
  16. RewriteRule ^vgt(.*)$           /photos/vgt.htm         [r=301,nc]
  17. </IfModule>

A Better Example: General Rules

For Charles’ site mod rewrite really shines. He has 80 or 90 files, one per week, that have been moved. (actually I did make the redirection files for all of these — it was part of the conversion program I wrote to strip the files apart and load the content into WordPress – ed) With a few rules we can handle all of these since the file names are the date, and the slug for WordPress will still be the date, though in a different format (note how lame I am at not handling the zero-padded month and day digits. God I suck).

(Note, the line breaks are added here to fit the rules onto my too narrow page)

  1. # ———————————————————
  2. # translate archives ex. 1-20-05.html is /2005-01-20/
  3. # also, send any malformed archives to “slide of the week”
  4. # ———————————————————
  5. RewriteRule ^s.*/[09]{4}/([09])([09])([09]+)\.html$
  6.   /20$30$10$2[r=301,nc]
  7. RewriteRule ^s.*/[09]{4}/([09])([09]+)([09]+)\.html$
  8.   /20$30$1-$2/   [r=301,nc]
  9. RewriteRule ^s.*/[09]{4}/([09]+)([09])([09]+)\.html$
  10.   /20$3-$10$2/   [r=301,nc]
  11. RewriteRule ^s.*/[09]{4}/([09]+)([09]+)([09]+)\.html$
  12.   /20$3-$1-$2/    [r=301,nc]
  13. RewriteRule ^slideoftheweek-archive.*$
  14.   /slide-of-the-week/ [r=301,nc]

top of page

See Also

I’ll keep saying it: these pages are for my own use, I forget things so quickly that I need this. Here’s some real pages that helped me: