简体   繁体   English

Vagrant上的mod_rewrite问题

[英]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. 我刚刚转向Vagrant,设置了一个LAMP堆栈,到目前为止我真的非常高兴。 The only issue I am experiencing is with mod_rewrite. 我遇到的唯一问题是mod_rewrite。 I have confirmed mod_rewrite is turned on and it's working for redirects. 我已经确认mod_rewrite已打开并且它正在用于重定向。

The following code is in my vhost file: 以下代码位于我的vhost文件中:

<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: 然后我在.htaccess文件中有以下内容:

<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 基本上它是如何“以前工作”是我运行http://www.devserver.local/tools/test_function.json并重写为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 (eg: http://www.devserver.local/tools.php ). 现在我在流浪汉上它加载tools.php但没有查询字符串tool (例如: http://www.devserver.local/tools.php )。 It's almost like there is some configuration that allows PHP files to run without the .php. 这几乎就像有一些配置允许PHP文件在没有.php的情况下运行。

I can get it to work if I rename tools.php to tools-test.php and update my .htaccess to: 如果我将tools.php重命名为tools-test.php并将我的.htaccess更新为:我可以使它工作:

<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. 所以它检测tools.php退出并在运行我的.htaccess文件之前加载该文件。

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. 我不认为这是一个特别的Vagrant问题,它可能只是Apache或mod_rewrite中的一个设置我需要打开/关闭或配置。 Has anyone experienced this before? 有谁之前经历过这个吗?

SOLUTION

As Jon Lin mentioned below, it was an issue with MultiViews being turned on. 正如Jon Lin在下面提到的,打开MultiViews是一个问题。 I opted for disabling it in my vhosts file instead of Jon's suggestion of disabling it directly from .htaccess. 我选择在我的vhosts文件中禁用它,而不是Jon建议直接从.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>

This sounds like a multiviews issue. 这听起来像一个多视图问题。 Multiviews is part of mod_negotiation and tries to "guess" a resource that a URL could map to. 多视图是mod_negotiation的一部分,并尝试“猜测”URL可以映射到的资源。 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. 因此,当它在URL中看到/tools/并且它看到文件/tools.php ,它会直接将请求映射到php文件,从而完全绕过mod_rewrite。

Try adding this to your htaccess file: 尝试将此添加到您的htaccess文件:

Options -Multiviews

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM