Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

.htaccess - Internal URL rewrite no longer working after upgrading Apache to 2.4

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]

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]

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

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

What am I missing?

EDIT: 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.

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...