mod_rewrite does not work as expected when "RailsAllowModRewrite on"

211 views
Skip to first unread message

Jonathan Julian

unread,
Apr 14, 2008, 3:37:38 PM4/14/08
to Phusion Passenger Discussions
# in server config
RailsAllowModRewrite on
# in Directory block
RewriteEngine On
RewriteRule alice.html bob.html

request /alice.html

from the rewrite_log, the rewrite has succeeded...(level 3)
127.0.0.1 - - [14/Apr/2008:15:26:07 --0400] [localhost/sid#80bbc0]
[rid#8c5450/initial] (3) [perdir /] strip per-dir prefix: /Users/
jjulian/trunk/public/alice.html -> Users/jjulian/trunk/public/
alice.html
127.0.0.1 - - [14/Apr/2008:15:26:07 --0400] [localhost/sid#80bbc0]
[rid#8c5450/initial] (3) [perdir /] applying pattern 'alice.html' to
uri 'Users/jjulian/trunk/public/alice.html'
127.0.0.1 - - [14/Apr/2008:15:26:07 --0400] [localhost/sid#80bbc0]
[rid#8c5450/initial] (2) [perdir /] rewrite 'Users/jjulian/trunk/
public/alice.html' -> 'bob.html'
127.0.0.1 - - [14/Apr/2008:15:26:07 --0400] [localhost/sid#80bbc0]
[rid#8c5450/initial] (3) [perdir /] add per-dir prefix: bob.html -> /
bob.html
127.0.0.1 - - [14/Apr/2008:15:26:07 --0400] [localhost/sid#80bbc0]
[rid#8c5450/initial] (1) [perdir /] internal redirect with /bob.html
[INTERNAL REDIRECT]

but, in the error_log, the original request was sent to Rails (level
debug)
[16340:Hooks.cpp:370] Processing HTTP request: /alice.html

I expect Rails to get a request for /bob.html. Is this a bug?
Shouldn't Passenger handle requests at the end of the chain *after*
they are rewritten?

Note - with "RailsAllowModRewrite off", no rewriting at all occurs.
TIA

Jonathan Julian

unread,
Apr 14, 2008, 6:15:53 PM4/14/08
to Phusion Passenger Discussions
Where this breaks in the real world is here:

RailsAllowModRewrite on
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L]

When the maint html page exists, all requests will be rewritten to "/
system/maintenance.html". Passenger should then see that those files
exist on the fs and serve then without invoking Rails. The bug is that
Passenger is using the *original* request URI instead of the rewritten
one. This also breaks with URIs like server-status and server-info, if
you have those features enabled.

On Apr 14, 3:37 pm, Jonathan Julian <jonathan.m.jul...@gmail.com>
wrote:

Jonathan Julian

unread,
Apr 15, 2008, 11:27:49 AM4/15/08
to Phusion Passenger Discussions
OK, the system/maintenance.html example can be made to work - simply
by putting it *outside* of the Directory block, in the server config.

There is still an issue where Passenger is sending the *original* uri
to Rails instead of the rewritten one. More examples below:

# in server config, not in Directory
RailsAllowModRewrite on
RewriteEngine On
RewriteRule ^/alice$ %{DOCUMENT_ROOT}/bob
RewriteRule ^/server-status$ - [L,H=server-status]

request /alice

127.0.0.1 - - [15/Apr/2008:11:21:26 --0400] [localhost/sid#80bbc0]
[rid#8cd850/initial] (2) init rewrite engine with requested uri /alice
127.0.0.1 - - [15/Apr/2008:11:21:26 --0400] [localhost/sid#80bbc0]
[rid#8cd850/initial] (3) applying pattern '^/alice$' to uri '/alice'
127.0.0.1 - - [15/Apr/2008:11:21:26 --0400] [localhost/sid#80bbc0]
[rid#8cd850/initial] (2) rewrite '/alice' -> '/Users/jjulian/trunk/
public/bob'
127.0.0.1 - - [15/Apr/2008:11:21:26 --0400] [localhost/sid#80bbc0]
[rid#8cd850/initial] (3) applying pattern '^/server-status$' to uri '/
Users/jjulian/trunk/public/bob'
127.0.0.1 - - [15/Apr/2008:11:21:26 --0400] [localhost/sid#80bbc0]
[rid#8cd850/initial] (2) local path result: /Users/jjulian/trunk/
public/bob
127.0.0.1 - - [15/Apr/2008:11:21:26 --0400] [localhost/sid#80bbc0]
[rid#8cd850/initial] (1) go-ahead with /Users/jjulian/trunk/public/bob
[OK]

yet, Rails gets /alice. I think it should get /bob
[18623:Hooks.cpp:370] Processing HTTP request: /alice

request /server-status

127.0.0.1 - - [15/Apr/2008:11:21:48 --0400] [localhost/sid#80bbc0]
[rid#8cd850/initial] (2) init rewrite engine with requested uri /
server-status
127.0.0.1 - - [15/Apr/2008:11:21:48 --0400] [localhost/sid#80bbc0]
[rid#8cd850/initial] (3) applying pattern '^/alice$' to uri '/server-
status'
127.0.0.1 - - [15/Apr/2008:11:21:48 --0400] [localhost/sid#80bbc0]
[rid#8cd850/initial] (3) applying pattern '^/server-status$' to uri '/
server-status'
127.0.0.1 - - [15/Apr/2008:11:21:48 --0400] [localhost/sid#80bbc0]
[rid#8cd850/initial] (2) remember /server-status to have Content-
handler 'server-status'
127.0.0.1 - - [15/Apr/2008:11:21:48 --0400] [localhost/sid#80bbc0]
[rid#8cd850/initial] (1) pass through /server-status
localhost - - [15/Apr/2008:11:21:48 --0400] [localhost/sid#80bbc0]
[rid#8cd850/initial] (1) force filename /Users/jjulian/trunk/public/
server-status to have the Content-handler 'server-status'

Rails gets this request. I think it should not, since a separate
Content-handler was set on it.
[18624:Hooks.cpp:370] Processing HTTP request: /server-status


On Apr 14, 6:15 pm, Jonathan Julian <jonathan.m.jul...@gmail.com>

Patrick Quinn-Graham

unread,
May 2, 2008, 3:36:22 PM5/2/08
to Phusion Passenger Discussions
I can confirm that I'm seeing this behaviour as well, and that I
consider it a bug.

My rails app relies heavily on httpd rewrites (currently deployed as a
bunch of mongrels behind lighttpd) to allow the same app to provide
different content to different domains - this makes Passenger unusable
for me right now.

On Apr 15, 4:27 pm, Jonathan Julian <jonathan.m.jul...@gmail.com>

RSL

unread,
May 7, 2008, 9:25:14 PM5/7/08
to Phusion Passenger Discussions
I've found a solution to this problem. The key is that there's a
slight difference in how the RewriteRule has to be specified in the
conf files. You can read the whole story here http://www.webmasterworld.com/apache/3335621.htm
but short answer is that you need to include the leading / in the
regex of the rule. Here's [basically] my vhost config which completely
works [you have no idea how excited I am by that fact] and hopefully
can serve as a good example.

<VirtualHost *:80>
ServerName backend.com
ServerAlias proba.com altra.com tertia.net
DocumentRoot /path/to/app/public

RewriteEngine On

# Basic content redirection
RewriteRule ^/$ /_sites/%{HTTP_HOST}/cache/index.html [QSA,L]
RewriteRule ^/([^.]+)$ /_sites/%{HTTP_HOST}/cache/$1.html [QSA,L]
RewriteRule ^/([^.]+).atom$ /_sites/%{HTTP_HOST}/cache/$1.atom
[QSA,L]
RewriteRule ^/([^.]+).xml$ /_sites/%{HTTP_HOST}/cache/$1.xml
[QSA,L]

# Asset redirection
RewriteCond %{HTTP_HOST} !backend.com
RewriteCond %{REQUEST_FILENAME} (files|images|javascripts|
stylesheets)/.*
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/(files|images|javascripts|stylesheets)/(.*)$ /_sites/%
{HTTP_HOST}/$1/$2 [QSA,L]
</VirtualHost>

Rock on with yr easy deploying selves! :)

RSL

RSL

unread,
May 7, 2008, 9:51:57 PM5/7/08
to Phusion Passenger Discussions
UPDATE: I was doing some tweaking and discovered that you don't even
need to enable RailsAllowModRewrite to get this as long as you're
putting yr rewrite rules in the conf files.

RSL
Reply all
Reply to author
Forward
0 new messages