[Rails] Avoid parsing URL in Apache conf?

Han Holl han.holl at informationslogik.nl
Fri Nov 19 09:50:59 GMT 2004


On Friday 19 November 2004 09:22, Ryan Platte wrote:
> The thought has been bugging me since I was looking at the url_for
> code a few days ago...
>
> The knowledge of how URLs are set up for a Rails site is split between
> the Apache conf and the Rails code. What if the mod_rewrite rules just
> sent the entire path into dispatch.*, rather than parsing the
> controller, action, and id parts? Then the same Rails code could be
> responsible for translating to and from Rails params.
>
> Something like (totally untested, don't actually use this):
> RewriteRule ^([-_a-zA-Z0-9]+/.*)$ /dispatch.cgi?path=$1 [QSA, L]
>
> Just the duplication of knowledge is smelly, but it seemed like the
> UrlRewriter code was less sure of what it was doing than the rest of
> the Rails code I've seen. I fixed a bug in UrlRewriter relating to the
> "index" action being the default, and guess where that policy is also
> expressed: mod_rewrite rules. Could be a coincidence, I suppose.
>
> Seems to me like there's a missing abstraction here. Or am I all wet?

Dispatching to mod_ruby, fcgi or plain cgi on basis of URL is quite nice 
though.
So maybe just three rewrite rules, instead of the current set.

Also, using mod_rewrite in a per directory setting is evil, and should, 
according to both Apache and mod_rewrite docs, be used only if there is 
absolutely no other alternative.

I can't use virtual hosts, so I've rewritten the .httpaccess rules to rewrite 
based only on the first element of the path of the URL. An advantage is that 
this can be dropped in /etc/httpd/conf.d without altering the main 
httpd.conf.
You just have to be a bit more careful with absolute and relative URL's in 
your templates.

Cheers,

Han Holl



More information about the Rails mailing list