M'sieur Toph'
M'sieur Toph'

Reputation: 2676

Two simple RewriteRule's in a row ... but strange result at the end

I am turning crazy with this .htaccess :

SetEnv PHP_VER 5_4
AddDefaultCharset UTF-8
Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L,R,QSA]

#--------------------------------------------------
# Format type management --------------------------
#--------------------------------------------------
RewriteRule ^(.*)\.(html|json|xml)$ $1?_format_=$2 [QSA,NC]

#--------------------------------------------------
# Route management --------------------------
#--------------------------------------------------
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule ^(.*)$ index.php?_route_=$1 [QSA,NC,L]

And I am trying to redirect :

http://mydomain/album/11.json

To

http://mydomain/index.php?_route_=album/11&_format_=json

Unfortunatley, I got this :

http://mydomain/index.php?_route_=album/11/11.json&_format_=json

I really dont understand what is wrong in theses tow rules and how comes this result ...

Any idea ?

Thx...


By the way, it works perfectly when I use this instead (add the second rule within the first one and add L flag), but this is not the way I want to achieve this :

(...)
#--------------------------------------------------
# Format type management --------------------------
#--------------------------------------------------
RewriteRule ^(.*)\.(html|json|xml)$ index.php?_format_=$2&_route_=$1 [QSA,NC,L]
(...)

Upvotes: 2

Views: 60

Answers (1)

anubhava
anubhava

Reputation: 785651

Great question, I must say.

You need DPI flag to solve your problem (it needs to discard old path info)

The DPI flag causes the PATH_INFO portion of the rewritten URI to be discarded.

Keep your rules like this:

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

#--------------------------------------------------
# Format type management --------------------------
#--------------------------------------------------
RewriteRule ^([^.]+)\.(html|json|xml)$ $1?_format_=$2 [DPI,QSA,NC]

#--------------------------------------------------
# Route management --------------------------
#--------------------------------------------------
RewriteRule ^((?!index\.php$).+)$ index.php?_route_=$1 [QSA,NC,L]

PS: I have also made some minor changes in your rules to make regex better.

Upvotes: 2

Related Questions