Reputation: 1912
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¶m=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.
Upvotes: 1
Views: 1096
Reputation: 29872
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.
Upvotes: 1