0

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

5
  • 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. Commented Apr 28, 2013 at 16:43
  • 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. Commented Apr 28, 2013 at 16:48
  • 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) Commented Apr 28, 2013 at 16:48
  • 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. Commented Apr 28, 2013 at 16:48
  • Can you give me some examples? Thanks :) Commented Apr 28, 2013 at 16:57

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

[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

7 Comments

I don't quite understand this. I got a url that is created dynamically, based on what is set for the item. Then on a template page I have conditionals that get whatever get variable is set in the url and show it on the front end. Basically this is a menu that gives locations prices.
Take your dynamic URL mysite.com/item/Bomba. Once it is requested then it would go through .htaccess and then take 'item/Bomba' and put it in the query string parameter: ?request=item/Bomba, which then goes through the url_handler.php script. Once in the script you can then perform any test on the URL you need too.
Do you mean that you always require the parameters; food, drink, bar and name, and then you want to make a short URL that only contains some of the required parameters so you want to default the missing ones to equal 1, so your page still works?
No, I retrieve whatever variables are in the url so maximum I can have maximum 4 and minimum 2 get variables. The food variable and name will always be present, and drink and bar will be optional, sometimes I will have just drink, sometimes bar and sometimes both. Because of that I don't know how to write a rewrite rule.
Presently I have something like this Options +FollowSymlinks RewriteEngine on RewriteRule ^item/(.+)/(.+)/(.+)/(.+) item/?food=$1&drink=$2&bar=$3&name=$4 [nc] RewriteRule ^item/(.+)/(.+)/(.+) item/?food=$1&bar=$2&name=$3 [nc] RewriteRule ^item/(.+)/(.+)/(.+)/ item/?food=$1&drink=$1&name=$3 [nc] But I don't know how to set when to take drink and when bar.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.