将Apache升级到2.4后,内部URL重写不再有效

时间:2013-11-16 20:38:54

标签: apache .htaccess mod-rewrite

我需要在内部重写基本URL,以便它不是/被映射到/index.php(默认行为),而是转到另一个名为contents.php的脚本。我在doc root中有一个简单的.htaccess:

RewriteEngine on
RewriteRule ^/?$ /contents.php?id_cat=0 [L]

这在Apache 2.2上运行良好,不再适用于Apache 2.4.6。现在它被忽略了,默认情况下最终会调用/index.php脚本。

但是,如果我添加Relocate标志:

RewriteEngine on
RewriteRule ^/?$ /contents.php?id_cat=0 [R,L]

...它确实有效,但它暴露了我不想要的重写URL。

我尝试使用PT(passthrough)标志[PT,L][END],但没有运气。

我错过了什么?

编辑:我启用了重写的跟踪日志记录,这就是我为单个请求所获得的:

[Sat Nov 16 17:43:22.021036 2013] [rewrite:trace3] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b67b0a0/initial] [perdir /var/www/bacityapartments/] strip per-dir prefix: /var/www/bacityapartments/
->
[Sat Nov 16 17:43:22.021075 2013] [rewrite:trace3] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b67b0a0/initial] [perdir /var/www/bacityapartments/] applying pattern '^/?$' to uri ''
[Sat Nov 16 17:43:22.021087 2013] [rewrite:trace2] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b67b0a0/initial] [perdir /var/www/bacityapartments/] rewrite '' -> '/contents.php?id_cat=0'
[Sat Nov 16 17:43:22.021096 2013] [rewrite:trace3] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b67b0a0/initial] split uri=/contents.php?id_cat=0 -> uri=/contents.php, args=id_cat=0
[Sat Nov 16 17:43:22.021111 2013] [rewrite:trace1] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b67b0a0/initial] [perdir /var/www/bacityapartments/] internal redirect with /contents.php [INTERNAL REDIRECT]
[Sat Nov 16 17:43:22.021197 2013] [rewrite:trace3] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b6790a0/subreq] [perdir /var/www/bacityapartments/] strip per-dir prefix: /var/www/bacityapartments/in
dex.html -> index.html
[Sat Nov 16 17:43:22.021208 2013] [rewrite:trace3] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b6790a0/subreq] [perdir /var/www/bacityapartments/] applying pattern '^/?$' to uri 'index.html'
[Sat Nov 16 17:43:22.021218 2013] [rewrite:trace1] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b6790a0/subreq] [perdir /var/www/bacityapartments/] pass through /var/www/bacityapartments/index.html
[Sat Nov 16 17:43:22.021268 2013] [rewrite:trace3] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b6740a0/subreq] [perdir /var/www/bacityapartments/] strip per-dir prefix: /var/www/bacityapartments/in
dex.cgi -> index.cgi
[Sat Nov 16 17:43:22.021290 2013] [rewrite:trace3] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b6740a0/subreq] [perdir /var/www/bacityapartments/] applying pattern '^/?$' to uri 'index.cgi'
[Sat Nov 16 17:43:22.021299 2013] [rewrite:trace1] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b6740a0/subreq] [perdir /var/www/bacityapartments/] pass through /var/www/bacityapartments/index.cgi
[Sat Nov 16 17:43:22.021345 2013] [rewrite:trace3] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b6790a0/subreq] [perdir /var/www/bacityapartments/] strip per-dir prefix: /var/www/bacityapartments/in
dex.pl -> index.pl
[Sat Nov 16 17:43:22.021355 2013] [rewrite:trace3] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b6790a0/subreq] [perdir /var/www/bacityapartments/] applying pattern '^/?$' to uri 'index.pl'
[Sat Nov 16 17:43:22.021364 2013] [rewrite:trace1] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b6790a0/subreq] [perdir /var/www/bacityapartments/] pass through /var/www/bacityapartments/index.pl
[Sat Nov 16 17:43:22.021389 2013] [rewrite:trace3] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b6740a0/subreq] [perdir /var/www/bacityapartments/] strip per-dir prefix: /var/www/bacityapartments/in
dex.php -> index.php
[Sat Nov 16 17:43:22.021398 2013] [rewrite:trace3] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b6740a0/subreq] [perdir /var/www/bacityapartments/] applying pattern '^/?$' to uri 'index.php'
[Sat Nov 16 17:43:22.021407 2013] [rewrite:trace1] [pid 5895] mod_rewrite.c(468): [client 127.0.0.1:55297] 127.0.0.1 - - [bacityapartments.localhost/sid#7f7e3b748400][rid#7f7e3b6740a0/subreq] [perdir /var/www/bacityapartments/] pass through /var/www/bacityapartments/index.php
00

所以重写正在进行,但随后就会继续进行,就好像它没有。

编辑2:如果我将此规则移至虚拟主机配置文件,它可以按预期工作。可以找到更多信息here

此外,此问题仅发生在目录本身。例如,如果我将/hello重写为/contents.php?msg=hello,它也可以从.htaccess中正常工作。

5 个答案:

答案 0 :(得分:13)

这是一个众所周知的bug in Apache 2.4。我使用了workaround provided here,现在正在使用。

答案 1 :(得分:11)

我有SEO链接的问题(确切地说Prestashop网站)。选项是添加

AllowOverride All 

在虚拟主机的目录中:

    <Directory /srv/path_to_public/public_html/>
            AllowOverride All
    </Directory>

来自apache docs:

http://httpd.apache.org/docs/current/mod/core.html#allowoverride AllowOverride无(2.3.9及更高版本),AllowOverride All(2.3.8及更早版本)

答案 2 :(得分:4)

升级到Apache 2.4.6后,我在虚拟主机上也有404s的WordPress重定向

在我的情况下,通过将每个虚拟主机目录配置的'AllowOverride None'更改为'AllowOverride FileInfo'来解决问题。

答案 3 :(得分:2)

我也偶然发现了这个非常烦人的问题。解决方案是通过为所需目录添加httpd.conf来禁用-MultiViews中的MultiViews,例如:

<Directory /home/example.com/public_html>
    Options -Indexes -MultiViews
    AllowOverride All
    Require all granted
</Directory>

之后,重写开始以预期的方式工作。

编辑配置文件后不要忘记重新加载Apache。

答案 4 :(得分:0)

就我而言,我是从 debian 8 (jessie) 升级到 9 (stretch)。

我一定是错误地覆盖了我之前的 /etc/apache2/apache2.conf 版本。

解决方案是改变它(在 /etc/apache2/apache2.conf 内):

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

进入这个:

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

...并且它神奇地修复了该机器上安装的每个 PHP 站点的所有重写问题:)