BASILIO
BASILIO

Reputation: 887

How to get multiply variables with mod_rewrite

How to make from http://site.com/id555/forum666/thread777 this http://site.com/#id=555#forum=666#thread=777? I have try this way:

RewriteEngine on
RewriteRule ^/id([0-9]+)/?$ /#id=$1 [NC,NE,R=303]
RewriteRule ^/forum([0-9]+)/?$ /#forum=$1 [NC,NE,R=303]
RewriteRule ^/thread([0-9]+)/?$ /#thread=$1 [NC,NE,R=303]

But all what i get with it is http://site.com/#id=555, so i tried make it like this way:

RewriteEngine on
RewriteRule ^(/(id|forum|thread)([0-9]+))+ #$2=$3 [NC,NE,N,R=303]

The result was http://site.com/#thread=777 . A do not understand, how i must change the rule, to get it on the right way. I use it from the main config over VirtualHost section, because it is much like for me, but if you help me to get it worked with .htaccess, I would be make it this way.

Important: It can be any amount of variables in URL in any order. Examples:

http://site.com/thread777/forum666
http://site.com/id555/thread777
http://site.com/forum666
http://site.com/id555/forum666/thread777/id333/id444/forum777

They all must be transformed this way like:

http://site.com/#thread=777#forum=666
http://site.com/#id=555#thread=777
http://site.com/#forum=666
http://site.com/#id555#forum=666#thread=777#id=333#id=444#forum=777

Upvotes: 1

Views: 91

Answers (1)

Andrew Schulman
Andrew Schulman

Reputation: 3544

I think your 2nd try was close. How about this:

RewriteRule ^(.*)/(id|forum|thread)([0-9]+)(.*) $1#$2=$3$4 [N,NC,NE,R=303]

As in your rule, the N flag makes the rule repeat as often as needed until it's done. At each iteration, it transforms the rightmost group of /id555 or whatever into #id=555.

^(.*) = leading groups, if any, that haven't been transformed yet

/(id|forum|thread)([0-9]+) = group being processed in this iteration

(.*) = trailing groups, if any, already processed

However I think that after this, the leading / may be gone, transformed to a #. So you may need to add it back in by following the above rule by

RewriteRule (.*) /$1

Upvotes: 1

Related Questions