阿帕奇升级到2.4后,内部URL重写不再工作重写、升级到、阿帕奇、工作

2023-09-02 09:49:12 作者:金木白头发好帅!

我需要在内部重写的基本URL,而不是/使被映射到的index.php(默认行为),它进入另一个名为脚本contents.php。我在文档根目录一个简单的.htaccess:

I need to internally rewrite the base URL so that instead of / being mapped to /index.php (default behaviour) it goes to another script named contents.php. I have a simple .htaccess in the doc root:

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

这是工作的罚款与Apache 2.2,并且不再使用Apache 2.4.6的作品。现在,它会被忽略和被调用默认的index.php脚本结束了。

That was working fine with Apache 2.2 and no longer works with Apache 2.4.6. Now it is simply ignored and the /index.php script ends up being called by default.

不过,如果我添加了移居标志:

However, if I add the Relocate flag:

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

...它的工作,但它暴露了重写URL,这是我不想要的。

... it does work, but it exposes the rewritten URL, which I don't want.

我尝试使用PT(直通)标志 [PT,L] [结束] ,但没有运气

I tried using the PT (passthrough) flag [PT,L] and [END] but no luck.

我在想什么?

编辑:我启用了跟踪记录的改写,这是我得到了的单的要求:

I enabled trace logging for rewrite and this is what I got for a single request:

[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

所以正在执行的重写,但如此下去,就好像没有。

So the rewriting is being performed, but then it goes on just as if it didn't.

编辑2:如果我提出这个规则的虚拟主机配置文件,它的工作预期。更多信息可以发现here.

EDIT 2: If I move this rules to the virtualhost configuration file, it does work as expected. More information can be found here.

此外,此问题只发生在目录本身。举例来说,如果我重写 /你好 /contents.php?msg=hello ,它工作正常距离的.htaccess了。

Also, this problem only happens for the directory itself. For instance, if I rewrite /hello to /contents.php?msg=hello, it works fine from .htaccess too.

推荐答案

这是Apache的一个已知错误2.4 。我用了workaround这里提供的,它现在的工作。

This is a known bug in Apache 2.4. I used the workaround provided here and it's working now.