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

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post by pickle »

Maybe installing MAMP would help.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply