Get Apache Serving Static Files With Prefixed Mongrel

I decided to run under a “/v1” URL prefix. It’s still beta, and the URL structure will likely change in the future. I don’t want to break 160,000 links when that day comes. Fortunately, Mongrel makes this pretty easy with the –prefix option to mongrel_rails.

I added --prefix '/v1' to my mongrel_rails command line. After removing absolute URLs (those not using {:controller=>...}) from my views, everything worked great.

The only problem is, this technique prevents Apache from serving static files without hitting the Mongrel server. A typical mod_rewrite directive like this:

RewriteRule (.*) $1 [L]

doesn’t work, because the REQUEST_URIs now all start with “/v1” and the files in “MyRailsApp/public” don’t.

Here’s a simple workaround: make a symlink like this:

cd /path/to/MyRailsApp/public
ln -s . v1

That’s right, “v1” is a symlink that points back to the “public” directory. I was afraid this might cause an infinite loop, but it doesn’t. Keeping the same mod_rewrite rule above, Apache will serve the static files under “public” even if the request is prefixed with “/v1”. Pretty cool.