bhttoan
bhttoan

Reputation: 2736

Apache 2.4 mod_rewrite and htaccess

I am in the process of migrating from PHP 5.3.3/Apache 2.2 to PHP 5.6.13/Apache 2.4 and am struggling with rewrite rules.

In my sub directory I have an htaccess file but the rewrite rules in it don't get applied.

Options +SymLinksIfOwnerMatch
RewriteEngine on
Rewritebase /dir/module/

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule ^edit/([0-9]+) /dir/module/edit.php?id=$1 [L]

I turned on rewrite logging and in this I can see an entry which looks as though the attempted rewrite is going to edit.php/186591 rather than edit.php?id=186591 but I don't know if that is normal or not:

[Wed Oct 21 00:34:52.207212 2015] [rewrite:trace3] [pid 19507] mod_rewrite.c(468): 
[client 192.168.1.1:43597] 1.2.3.4 - - [www.domain.com/sid#7f057ae2d120
[rid#7f057b094380/subreq] [perdir /home/domain/public_html/dir/module/] 
applying pattern '^edit/([0-9]+)' to uri 'edit.php/186591'

Where am I going wrong?

Upvotes: 2

Views: 1075

Answers (1)

hjpotter92
hjpotter92

Reputation: 80639

The problem was due to the enabled MultiViews option. Put the following in server config file:

AllowOverride Options=All,MultiViews

and then, in the htaccess file, disable the MultiViews directive:

Options +SymLinksIfOwnerMatch -MultiViews

This should effectively fix the problem. I'd also go on to say (same as I commented above) that you should put more specific rules prior to any general rule in your htaccess files. Thus,

RewriteEngine on
Rewritebase /dir/module/

RewriteRule ^edit/([0-9]+)$ edit.php?id=$1 [L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

Upvotes: 1

Related Questions