Bojan Petkovski
Bojan Petkovski

Reputation: 21

Rewrite url with multiple get variables

I have a url that looks like this http://mysite.com/item/?food=1&drink=1&bar=1&name=Bomba

I want to make it more friendly and maybe more secure and to look something like http://mysite.com/item/Bomba

The problem is that sometime drink or bar will not be part of the url, so I don't know how to make it to work with .htaccess. Also I don't know how to make rules for multiple get variables and if I can use conditionals in a rewrite rule (if drink==true or something similar). And also I don't want to use post because I want to be able to share the link.

So far I made something like this

mysite.com/item/1/Bomba.menu

and the rule

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^item/(.+)/(.+).menu item/?food=$1&drink=$1&bar=$1&name=Bomba [nc]

But it only works if the url stays the same. Thanks

Upvotes: 0

Views: 994

Answers (2)

Bojan Petkovski
Bojan Petkovski

Reputation: 21

So at last I made this short link http://mysite.com/item/1/D1/W1/Bomba that is taking me here http://mysite.com/item/?food=1&drink=1&wine=1&name=Bomba And added this rules to the .htaccess file.

Options +FollowSymlinks  
RewriteEngine on  
RewriteRule ^item/(.+)/(D.)/(W.)/(B.+)/(.+) item/?food=$1&drink=$1&wine=$1&bar=$1&name=$5 [nc]
RewriteRule ^item/(.+)/(D.+)/(W.+)/(.+) item/?food=$1&drink=$1&wine=$1&name=$4 [nc]
RewriteRule ^item/(.+)/(D.+)/(B.+)/(.+) item/?food=$1&drink=$1&bar=$1&name=$4 [nc]
RewriteRule ^item/(.+)/(W.+)/(B.+)/(.+) item/?food=$1&wine=$1&bar=$1&name=$4 [nc]

I hope that it will help somebody. :)

Upvotes: 1

Springie
Springie

Reputation: 728

[EDITED Answer]

You would need to have them set as key/value pairs as you have no way to tell if is a Drink or a Bar ID.

The other way I can think of, which won't look quite as elegant is to prefix the ids, so if it was a Drink ID then make the dynamic URL:

http://mysite.com/item/D1/B4

Then if it is prefixed with 'D' then it is a drink, 'B' for bar etc

Upvotes: 0

Related Questions