KBD

Keith Devens .com

Monday, December 1, 2008 Flag waving
All non-trivial abstractions, to some degree, are leaky. – Joel Spolsky (The Law of Leaky Abstractions)
← Buffy Radio Station!??!I win! :) →

Daily link icon Saturday, August 17, 2002

Fun with mod_rewrite

I've been learning mod_rewrite today. I'm thinking of writing a CMS, and I need to be able to translate "fake" URLs (requests for files that don't actually exist) into objects in my database. Importantly, however, I want to make sure that if an actual file exists, that file gets served instead of looking in my CMS. So, here are my rules:

RewriteEngine On
RewriteBase /testing/rewritetest/
RewriteRule ^([^.]+[^/])$ $1/ [R=permanent,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ bar.php?$1

Basically, this says "start the rewriting engine" (start processing rules) and set the base directory. If the thing you requested looks like a directory (if it doesn't have an extension), then add a trailing slash and make it look like a directory. This sends an actual redirect (code 301, "moved permanently") to the browser, and stops processing other rules (L means "last").

Otherwise, if it's either a "real looking" directory with a slash on the end, or it looks like a file (has an extension), then if what you requested isn't a real directory and isn't a real file, fulfill the request with bar.php and pass it the actual requested URL through the querystring.

This is, of course, just a test, and filenames, etc. will be changed for my actual CMS.

There was a bug in Apache for Windows that caused this to break when I put it on my system. I found this in the release announcement for Apache 1.3.26, which happens to be the version I'm using:

Fix for a problem in mod_rewrite which would lead to 400 Bad Request responses for rewriting rules which resulted in a local path.

Even though they said the bug was fixed, this seemed to be the same behavior I was getting. In fact, the engine was erroneously injecting the local path into the filename twice when it resolved the URL into a filename. As you can see in the log I generated (some info deleted):

(3) [per-dir c:/prog/work/localweb/testing/rewritetest/] add per-dir prefix: bar.php -> c:/prog/work/localweb/testing/rewritetest/bar.php
(3) [per-dir c:/prog/work/localweb/testing/rewritetest/] add per-dir prefix: c:/prog/work/localweb/testing/rewritetest/bar.php -> c:/prog/work/localweb/testing/rewritetest/c:/prog/work/localweb/testing/rewritetest/bar.php

So, that's definitely wrong. But I noticed they said the bug was for when you resolved to a local path, so I cheated and changed it to an absolute path:

RewriteRule ^(.+)$ http://localhost/testing/rewritetest/bar.php?$1

And that fixed it.

← Buffy Radio Station!??!I win! :) →

Comments XML gif

Dylan Williams wrote:

You are a star! This has been bugging me for days!!

∴ Dylan Williams | 18-Oct-2006 11:12am est | #9731

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

December 2008
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

new⇒Free image hosting sites

Well, TinyPic has this in its​FAQ:

> Images and videos is in​your accoun...

Keith: Dec 1, 1:13am

new⇒Join a NameValueCollection into a querystring in C#

Well with a lamba expression, this​is what I came up​with:

?!code:csharp...

Gustaf Lindqvist: Nov 30, 4:38pm

Why no generic OrderedDictionary?

Check​http://www.codeproject.com/KB/recip​es/GenericOrderedDictionary.aspx?d...

Gabrielk: Nov 27, 6:57am

WhatIsMyIP.com

http://www.thesysteminfo.com is​another good alternate to​whatismp.com... I...

Kripz: Nov 26, 8:51pm

Girls, please don't get breast implants

Actually I think it's sweet when a​man loves a woman whether she's big​or n...

218.186.12.228: Nov 26, 9:40am

Generated in about 0.397s.

(Used 8 db queries)

mobile phone