views:

91

answers:

2

I am a newbie to mod_rewrite and I was wondering if there is any way you could make on rewrite script that handles and number of variables you throw at it.

Example:

www.krisnicolaou.com/index.php?id=5&sort=asc&limit=25&param=first_name

to

www.krisnicolaou.com/5/asc/25/first_name

...but, you can also pass these variables in on another page and it would work with that one script:

www.krisnicolaou.com/index.php?page=view&action=add

to

www.krisnicolaou.com/view/add/

I essentially don't want to be limited as to how many parameters I can add on to the end and not have to constantly modify the .htaccess file.

Thanks in advance.

+1  A: 

Something like this.

great_llama
Torez and great_llama, so one of you linked to it, and the other accepted the link as the answer but neither of you thought the original answer was worth an upvote? Where's the love?
bmb
+1  A: 

Usually one want to take 'clean' urls, and covert them to parameters. What you are asking for is the opposite. Here's a tested ruleset.

RewriteEngine on

RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} ^([^=]*)=([^&]*)(&.*)?
RewriteRule ^(.*/)?([^/]+) $1%2/$2?%3 [L]

This will run if there are parameters, and for each param, it will add it to the URL and remove it from the param list. The [N] will cause it to run until there are no more parameters.

To test, I create the following structure:

view
view/add
view/add/index.htm

I put the above rules in a .htaccess file.

Normal test: http://www.theeggeadventure.com/2009/index.htm?page=view&action=add

Additional params (404) test http://www.theeggeadventure.com/2009/index.htm?page=view&action=add&foo=bar URL /2009/view/add/bar/index.htm was not found on this server.

brianegge
Hey, thanks for the quick response but for some reason its not working. Any idea why? And don't say "did you turn on the rewrite engine" :)Thanks
Torez