Calling mod_rewrite Gurus…

One of the trade-offs to going with a fully WordPress driven site for content is a little URL awkwardness for image and archive files you want to show/link to on a page.

For example, in a “standard” site it is pretty easy to have a page at this location:

and a related file download at this location:

If you are using a CMS like WordPress to create the page here: (or

then there is no existing directory structure to allow you to put the ‘download/’ into.

You can set up custom rewrite rules for each file you reference, but I prefer the “set it and forget it” approach rather than the “micro maintenance” approach. The solution I’m using is fairly simple.

I’m setting up a psuedo-mirror of the site WordPress page structure under a site/ directory. This means that in the example above, I will place the file in a directory like this:


I then need to rewrite the /foo/download/ URL to point to the /site/foo/download/ file.

The WordPress rules only kick in when a file doesn’t exist, so you might think I should be able to simply put the file at /foo/download/ Unfortunately, directory level URLs (/foo/) will get caught as “existing” too, keeping the WordPress rules from kicking in. Using the rewrite into a sub-directory solves this.

This only needs to happen for files with file extensions like .php, .html, .zip, .tar.gz, etc. so I need (what should be) a rather simple condition (RewriteCond) and rule (RewriteRule). Unfortunately, I’ve been banging my head against this for too long and I haven’t gotten it to work.

If your mod_rewrite-fu is stronger than mine (or if you have a different solution), perhaps you can help me in the comments. 🙂

If you are a mod_rewrite guru and are willing to be a go-to resource for me on mod_rewrite issues (paid, of course), let me know. I waste way too much time futzing with this stuff trying to get it to work.

UPDATE: I now have a very good working solution thanks to Luke Baker. His approach is a lot better than mine was: check to see if the file exists in my psuedo-site structure (DocumentRoot/site/file.gif), then rewrite the URL if it does. Here is the working rewrite code:

RewriteCond %{DOCUMENT_ROOT}site%{REQUEST_URI} -f
RewriteRule (.*) /site%{REQUEST_URI} [L]

Thanks Luke! 🙂