views:

34

answers:

2

Even simple .htaccess gives me headaches and I need to do the following generic mapping:

http://example.com/project/controllername/key1/val1/key2/val2/.../keyN/valN

-->

http://example.com/project/controllername.xyz?key1=val1&key2=val2...&keyN=valN

example:

http://example.com/so/pagecontroller/id/1/time/12345/title/helloworld

-->

http://example.com/so/pagecontroller.xyz?id=1&time=12345&title=helloworld

Any guidance will help! Especially with handling special chars like '/', '?' and '&' (more?) in keys and values.

EDIT: To clarify, 'project' and 'controllername' paths are dynamic - they are not static. Also the number of keys and values is not pre-determined! I need help in creating the htaccess file code and where to place this file in the web tree and if apache needs restarting everytime the htaccess file is modified. Thanks!

A: 

As long as you don't want to select those special characters, they should be no problem. A rule for your example might be:

RewriteRule http://example.com/so/pagecontroller/id/([0-9]+)/time/([0-9]+)/title/(.*)$ http://example.com/so/pagecontroller.xyz?id=$1&time=$2&title=$3  

I don't think you can have a dynamic number of variables in a rewrite rule. But what you can do is the following:

RewriteRule http://example.com/so/pagecontroler/(.*) http://example.com/so/pagecontroler.xyz?vars=$1

Than you have a GET parameter with the name "vars" and the rest of the query as a value. You can than split the different keys and values server side e.g. with the explode() function of PHP.

Kau-Boy
Thanks but I cannot hardcode the keys or their positions.
Yehonatan
Yes, I suppose I can supply all keys and values as single parameter.
Yehonatan
A: 

Try these rules:

RewriteRule ^([^/]+/[^/]+)/([^/]+)/([^/]+)(/.+)?$ $1$4?$2=$3 [QSA,N]
RewriteCond $1 !.+\.xyz$
RewriteRule ^([^/]+/[^/]+)$ $1.xyz [L]
Gumbo
Thanks, I will test this as soon as I can. Will this handle a dynamic number of keys and values?
Yehonatan