Mod_rewrite issue on Vagrant
I just switched to Vagrant, installed the LAMP stack and am very happy so far. The only problem I ran into was mod_rewrite. I have verified that mod_rewrite is enabled and it works for redirects.
The following code is in my vhost file:
<VirtualHost *:80>
ServerName devserver.local
DocumentRoot /var/www/public_html
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/vagrant_dev-error.log
LogLevel warn
CustomLog /var/log/apache2/vagrant_dev-access.log combined
</VirtualHost>
In my .htaccess file I have the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tools\/([a-zA-Z_]+)\.json$ tools.php?tool=$1 [QSA,L]
</IfModule>
Basically, the way it "worked" is I run http: //www.devserver.local/tools/test_function.json and overwrite http: //www.devserver.local/tools.php? Tool = test_function
Now when I am on vagrant it downloads tools.php but without the query string tool
(ex: http: //www.devserver.local/tools.php ). It almost looks like some kind of configuration that allows PHP files to work without .php.
I can get it to work if I rename tools.php to tools-test.php and update my .htaccess to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tools\/([a-zA-Z_]+)\.json$ tools-test.php?tool=$1 [QSA,L]
</IfModule>
So it detects tools.php exits and loads that file before running my .htaccess file.
I don't think this is a vagrant issue, it might just be a setting in Apache or mod_rewrite. I need to enable / disable or configure. Has anyone experienced this before?
Decision
As John Lean mentioned below, it was an enable issue MultiViews
. I decided to disable it in my vhosts file instead of Jon's suggestion to disable it directly from .htaccess. Both worked.
<VirtualHost *:80>
ServerName devserver.local
DocumentRoot /var/www/public_html
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/vagrant_dev-error.log
LogLevel warn
CustomLog /var/log/apache2/vagrant_dev-access.log combined
</VirtualHost>
source to share
This sounds like a problem with multiple views. Multiviews is part of mod_negotiation and tries to "guess" the resource that the URL might refer to. So when it sees /tools/
in the url and sees the file /tools.php
, it automatically maps the request to the php file and thus bypasses mod_rewrite entirely.
Try adding this to your htaccess file:
Options -Multiviews
source to share