0
votes

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

2
you want to remove the variables from a GET request and then share the link? This is impossible. If you have some specific variables you want to remove from the GET url, then you just have to edit your code to check whether or not they're specified in the GET request (and behave consequently). Post some code and we might help.Saturnix
No, no I don' want to remove the variables. It is a dynamically created link, so sometimes some of the variables will not be present, for example some links will be mysite.com/item/?food=1&drink=1&bar=1&name=Bomba, other mysite.com/item/?food=1&bar=1&name=Pistol etc.Bojan Petkovski
One solution would be to have the url segments as pairs. So mysite.com/item/?food=1&drink=1&bar=1&name=Bomba would become mysite.com/item/food/1/drink/1/bar/1/name/Bomba or do it like this: mysite.com/item/Bomba?food=1&drink=1. Use a routing library like klein, for example (github.com/chriso/klein.php)juuga
Sounds like you should build your own controller in PHP, and redirect all URL's to the controller. Then you can do stuff like conditionals, and use whatever words you like in the URL's.adeneo
Can you give me some examples? Thanks :)Bojan Petkovski

2 Answers

1
votes

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. :)

0
votes

[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