Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite issue on Vagrant

I've just made the move to Vagrant, have set-up a LAMP stack and am really pleased so far. The only issue I am experiencing is with mod_rewrite. I have confirmed mod_rewrite is turned on and it's working 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>

I then have the following in my .htaccess file:

<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 how it "used to work" is I run http://www.devserver.local/tools/test_function.json and it rewrites to http://www.devserver.local/tools.php?tool=test_function

Now that I am on vagrant it loads tools.php but without the query string tool (e.g: http://www.devserver.local/tools.php). It's almost like there is some configuration that allows PHP files to run without the .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 it is specifically a Vagrant issue, it might just be a setting in Apache or mod_rewrite I need to turn on/off or configure. Has anyone experienced this before?

SOLUTION

As Jon Lin mentioned below, it was an issue with MultiViews being turned on. I opted for disabling it in my vhosts file instead of Jon's suggestion of disabling 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>
like image 515
Ben Sinclair Avatar asked Jan 01 '26 09:01

Ben Sinclair


1 Answers

This sounds like a multiviews issue. Multiviews is part of mod_negotiation and tries to "guess" a resource that a URL could map to. So when it sees /tools/ in the URL and it sees the file /tools.php, it'll outright map the request to the php file and thus completely bypass mod_rewrite.

Try adding this to your htaccess file:

Options -Multiviews
like image 173
Jon Lin Avatar answered Jan 03 '26 15:01

Jon Lin



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!