Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apache2 gives 404 before rewrite

i have a rewrite rule in my .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^vt$ vt.php [L]
</IfModule>

if file 'vt' exists in my website directory, the rewrite is done well. but if file 'vt' doesn't exist, apache2 just gives a 404 error.

so it seems apache2 do rewrite after it checks file stat. How do I fix this?


I think this is a bug. I use this rule finnally to avoid use two files (vt and vt.php).

RewriteRule ^vt$ - [H=application/x-httpd-php]
like image 577
user2902980 Avatar asked Mar 24 '26 00:03

user2902980


1 Answers

This sounds like there may be 2 things happening here.

  1. Mod_rewrite doesn't appear to be loaded
  2. Multiviews is making it appear as if mod_rewrite is loaded

What Multiviews does, is part of mod_negotiation which tries to "guess" what the request is for if it is missing things like an extension. So with Multiviews turned on, if it sees a request for /vt and the file vt doesn't exist, it guesses and finds /vt.php and serves that, otherwise it doesn't nothing and returns a 404. This would make it appear as if the mod_rewrite rule is actually being applied. If mod_rewrite is actually turned on, then it would apply the rule even if mod_negotiation was turned on and the file was missing (which would result in a 404).

So 1. make sure that mod_rewrite is actually loaded. To do this, check your httpd.conf file and there should be a line that looks like this (uncommented):

LoadModule rewrite_module modules/mod_rewrite.so

Or you can try removing the <IfModule mod_rewrite.c> container, if you get a 500 server error, mod_rewrite isn't loaded.

Then 2. add this line in your htaccess file to turn off Multiviews:

Options -Multiviews
like image 187
Jon Lin Avatar answered Mar 26 '26 15:03

Jon Lin