3Nex
3Nex

Reputation: 527

How to write a htaccess rewrite rule to get links like this?

I would like a rewrite rule to have the following behavior.

If the URL is:

/users

it would rewrite with index.php?template=users

And if it's:

/users?id=2

it would rewrite with index.php?template=users&id=2 for however many variables are in the URL. So that means that even if i used

/users?id=2&action=edit&something=no&something_else=yes

i'd want all of those variables to be passed, thus getting /index.php?template=usersid=2&action=edit&something=no&something_else=yes


Until now, my htaccess rule has always been:

RewriteRule ^([^\.]+)$ index.php?template=$1

But with that, i cannot use URL's with a question mark in them like i'd want to. Instead, i'd have to use /users&id=2, which looks silly.

I have googled quite a bit, but the solutions either don't work, or aren't similar enough to my case for me to adapt them and get what i want.

Upvotes: 0

Views: 31

Answers (1)

Prix
Prix

Reputation: 19528

What you want is the QSA flag, query string append which will add the rest of the query string to the redirect rule if you have more parameters:

RewriteRule ^([^/]+)$ /index.php?template=$1 [QSA,L]

Upvotes: 1

Related Questions