Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache does not rewrite request if file on path exists

I'm doing a rewrite with mod_rewrite on every request that does not match an existing file or directory. This is my configuration:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [NC,L]

This is used to map URLs like /abc/foo or /abc/foo/10 to my app. And it works just fine.

To improve the performance, my app now stores the results of a call to /abc/foo in a file foo in the corresponding directory /abc - so that after the first call the rewrite conditions do no longer apply (file does not exist) and apache directly serves the data without first invoking the app. Works fine as well.

The problem is: Requesting /abc/foo/10 does now no longer cause the URL to get rewritten, instead I get an error "404 File Not Found". The log entries state that the rewrite condition !-f is no longer true, but actually the file /abc/foo/10 does not exist. /abc/foo exists, but is a file, not a directory.

How can I get this to work?

(MultiViews is disabled)

like image 601
Niko Avatar asked Feb 18 '26 01:02

Niko


2 Answers

This is because foo exists as a file and apache serves foo with the additional /10 passed as a query string. So, your application should write some additional code to the foo file, that also checks if a request includes some additional url component and then handle creation of the directory "foo" and the file 10.

like image 66
Brian P Johnson Avatar answered Feb 19 '26 13:02

Brian P Johnson


You must be in per-dir/htaccess context w/ AcceptPathInfo on.

Therefore REQUEST_FILENAME matched the part that existed, and is not the same as REQUEST_URI.

Use the REQUEST_URI var if you don't care where the request was previously mapped in your rewritecond.

In per-vh context, these vars are always the same.

like image 41
covener Avatar answered Feb 19 '26 15:02

covener



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!