Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule and subdirectory

         

pedz

6:04 am on Oct 14, 2007 (gmt 0)

10+ Year Member



I have this:

<VirtualHost ...
<Directory "/usr/local/www/rcm.example..com/rails/current/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

RewriteEngine On
RewriteLog "/usr/local/www/rcm.example..com/logs/rewrite_log"
RewriteLogLevel 5

# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]
</VirtualHost>

If I request "foo", I get foo.html

Here is the rewrite log:

(2) init rewrite engine with requested uri /foo
(3) applying pattern '^([^.]+)$' to uri '/foo'
(2) rewrite '/foo' -> '/foo.html'
(2) local path result: /foo.html
(2) prefixed with document_root to /usr/local/www/rcm.example..com/rails/current/public/foo.html
(1) go-ahead with /usr/local/www/rcm.example..com/rails/current/public/foo.html [OK]

(Notice that the next to the last line is to prefix with the document root.)

If I request "subdir/foo", I get a forbidden (403) error. The rewrite log is:

(2) init rewrite engine with requested uri /system/foo
(3) applying pattern '^([^.]+)$' to uri '/system/foo'
(2) rewrite '/system/foo' -> '/system/foo.html'
(2) local path result: /system/foo.html
(1) go-ahead with /system/foo.html [OK]

The error log (for 4) simply has:

client denied by server configuration: /system/foo.html

Why does "foo" get prefixed with document root but "subdir/foo" does not?

Thank you,
pedz

[edited by: jdMorgan at 3:24 pm (utc) on Oct. 14, 2007]
[edit reason] example.com [/edit]

jdMorgan

3:27 pm on Oct 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's very odd...

Is the /system folder special in any way? -- For example, is it proxied, Aliased, or ScriptAliased elsewhere in your config file?

The only thing I see that I'd change is that you are missing an [L] flag on the rule, but I don't see how that would affect the prefixing of the DocumentRoot path.

Jim

pedz

3:45 pm on Oct 14, 2007 (gmt 0)

10+ Year Member



I finally tracked this down by adding debugging to the server.

The rewrite code, I guess as a final thing it does maybe, calls stat_prefix passing it the replaced pattern -- /system/foo.html in this case. If the new string has a slash in it, then it stats what turns out to be the first directory: /system in this case.

If that directory exists, then rewrite assumes you are trying to create an absolute path and does NOT prefix it with DOCUMENT_ROOT. It then tries to access /system/foo.html (as an absolute path), which fails due to permission and config issues. On a Mac system, /system exists. (Its called /System but the Mac file system is case insensitive usually.)

I changed my rule to be:

RewriteRule ^([^.]+)$ %{DOCUMENT_ROOT}$1.html [QSA]

This works. I had other rules that were basically doing the same thing and I had to fix those as well. The four or five paths I was testing now all work as I want them to.

I was tempted to open an Apache bug report. But, I don't know how else they could do it. It is just a subtle trap that people can fall in to. I got the code from the Ruby on Rails book. I posted an "errata" to the book just now. And, I think I'm going to post a message to the Rails Forum because I bet others fall into this too.