Torez
Torez

Reputation: 1912

mod_rewrite for any number of variables

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.

Upvotes: 1

Views: 1096

Answers (2)

brianegge
brianegge

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

great_llama
great_llama

Reputation: 11729

Something like this.

Upvotes: 1

Related Questions