Page 1 of 1

Can't figure out why RewriteCond with !-f doesn't work

Posted: Wed Apr 21, 2010 7:21 pm
by Chris Corbyn
I'm trying to get a FastCGI application up and running where the CGI itself is outside of the web root.

I can get the FastCGI app to load just fine such that *anything* in the URL is passed along to the app. However, I want to exclude static resources so I tried adding a RewriteCond using !-f on the REQUEST_FILENAME. It just seems to ignore it entirely and the FastCGI app ends up taking the request instead of Apache serving up the file.

Code: Select all

AppClass /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest -port 5065
FastCgiExternalServer /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest.fcgi -host 127.0.0.1:5065
<VirtualHost *:80>
    ServerName cioccolata-test.webdev
    DocumentRoot "/Sites/CioccolataTest.webapp/Contents/Resources/static"

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/(.*)$ /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest.fcgi/$1 [QSA,L]
</VirtualHost>
I have placed a text file in the static directory, named "hello.txt" with just the word "Boo" in it. I should be able to access /hello.txt and see that text file, however the -f rule isn't taking care of it.

So out of desperation (and for the sake of my own sanity) I wrote a one-off rule for paths starting with "/hello". Low and behold, this correctly serves up the text file and all other URLs route through to my FastCGI application. Which begs the question, why doesn't the previous rule work?

Code: Select all

AppClass /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest -port 5065
FastCgiExternalServer /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest.fcgi -host 127.0.0.1:5065
<VirtualHost *:80>
    ServerName cioccolata-test.webdev
    DocumentRoot "/Sites/CioccolataTest.webapp/Contents/Resources/static"

    RewriteEngine On

    RewriteCond %{REQUEST_URI} !^/hello
    RewriteRule ^/(.*)$ /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest.fcgi/$1 [QSA,L]
</VirtualHost>
I'm at a loss :? I've set so many RewriteRules up like this over the years (granted, not for a FastCGI app) and cannot see what I'm missing.

Re: Can't figure out why RewriteCond with !-f doesn't work

Posted: Wed Apr 21, 2010 10:40 pm
by Chris Corbyn
Here's a simplified version (removed the FastCgiExternalServer directive since this is all local at the moment):

Code: Select all

FastCgiServer /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest -port 5065
<VirtualHost *:80>
    ServerName cioccolata-test.webdev
    DocumentRoot /Sites/CioccolataTest.webapp/Contents/Resources/static

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/(.*)$ /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest/$1 [QSA,L]
</VirtualHost>

Re: Can't figure out why RewriteCond with !-f doesn't work

Posted: Thu Apr 22, 2010 3:10 am
by Chris Corbyn
Using %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f worked. That's weird... I've never had to specify absolute paths to apache before, it usually deals with paths relative to the document root.

Re: Can't figure out why RewriteCond with !-f doesn't work

Posted: Thu Apr 22, 2010 10:03 am
by pickle
It may just be your setup? I've never had to use anything but REQUEST_FILENAME either.

FYI - my .htaccess uses -s and -d rather than -f.

Re: Can't figure out why RewriteCond with !-f doesn't work

Posted: Thu Apr 22, 2010 9:25 pm
by Chris Corbyn
pickle wrote:It may just be your setup? I've never had to use anything but REQUEST_FILENAME either.

FYI - my .htaccess uses -s and -d rather than -f.
Yeah I think there's something weird with my Apache setup. It's just whatever the default Mac OS X (Snow Leopard) install is. I'm not too fussed now; I've learnt something to watch out for anyway.

Re: Can't figure out why RewriteCond with !-f doesn't work

Posted: Fri Apr 23, 2010 9:45 am
by pickle
Maybe installing MAMP would help.