18

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.

Community
  • 1
  • 1
LexLythius
  • 1,904
  • 1
  • 12
  • 20
  • 1
    Do you find something in the error log? Is mod rewrite module activated in the server configuration? – rekire Nov 16 '13 at 20:41
  • No errors. Mod_rewrite is enabled (which is why adding the R flag does work) and also AllowOverride All directive and FollowSymLinks directives are set up. – LexLythius Nov 16 '13 at 20:43
  • Your .htaccess files are ignored by apache2? – jacouh Nov 16 '13 at 21:30
  • @jacouh No, they're not being ignored. Read the info I posted and you'll see the rewrite is being made but then ignored. – LexLythius Nov 16 '13 at 21:32

5 Answers5

13

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

Community
  • 1
  • 1
LexLythius
  • 1,904
  • 1
  • 12
  • 20
  • Save my day! Almost the same issue here http://stackoverflow.com/questions/25527411/fail-to-rewrite-url-and-append-parameters-in-htaccess/25543010. – ChandlerQ Aug 28 '14 at 07:59
  • Out of curiosity, how did you manage to find this bug posted on apache bugzilla? I've spent like hours Googling but no luck... – ChandlerQ Aug 28 '14 at 08:06
  • @AmareKnight I think I googled something like "Apache 2.4 bug rewrite broken" but I can't be sure. Finding existing bugs in Bugzilla is not very easy, indeed. – LexLythius Sep 04 '14 at 15:33
  • Spent hours on this... Thanks – Avi Kehat Mar 16 '17 at 10:47
11

I had problems with SEO links (Prestashop site to be exact). Option was to add

AllowOverride All 

in virtual host's directory:

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

From apache docs:

http://httpd.apache.org/docs/current/mod/core.html#allowoverride AllowOverride None (2.3.9 and later), AllowOverride All (2.3.8 and earlier)

Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109
  • 1
    Be aware that setting permissive AllowOverride directives *may* lead to security issues depending on how your site scripts are written and what access different users have: http://serverfault.com/questions/224078/security-question-about-httpd-conf-allowoverride-setting – LexLythius Nov 14 '14 at 15:28
  • 1
    "AllowOverride All" in document root's Directory block worked for me. – estornes Jan 19 '15 at 17:53
  • 1
    On ubuntu with apache 2.4 in apache2.conf folder "/var/www/" is set `AllowOverride None`. On site config you have to overwrite this with `AllowOverride All ` if your document root is a folder in /var/www/. Otherwise your .htaccess is not working at all. thanks for this hint – Martin Krung Aug 25 '15 at 09:56
4

I also had 404s with WordPress redirects on virtual hosts after upgrading to Apache 2.4.6

In my case problem was solved by changing 'AllowOverride None' to 'AllowOverride FileInfo' for each virtual host directory config.

maratbn
  • 188
  • 1
  • 9
2

I have stumbled upon this very annoying issue as well. The solution is to disable MultiViews in httpd.conf, by adding -MultiViews for needed directory, for example:

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

After that, rewrites started to work expected way.

Don't forget to reload Apache after editing configuration files.

Community
  • 1
  • 1
Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
  • @FabianThommen You're very wrong, Fabian!! What you have said, could only give you any effect, if in your local `.htaccess` you have set `-MultiViews` on one of the directories. Directive that you say, just will let override using `.htaccess`. Think again! – Ilia Ross Aug 25 '15 at 14:10
  • 1
    ok, sorry I reread the question and deleted my comment and took back my downvote. – Martin Krung Aug 25 '15 at 16:27
0

In my case, I was upgrading from debian 8 (jessie) to 9 (stretch).

I must have mistakenly overrwritten my previous /etc/apache2/apache2.conf version.

The solution was to change this (inside /etc/apache2/apache2.conf) :

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

Into this:

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

...and it magically fixed all rewrite issues for every PHP site installed on that machine :)

arispen
  • 2,832
  • 1
  • 15
  • 13