0

I have a GET request as

http://www.example.com/index.php?location=location&category=category&keyword=any+keyword

I have converted this URL to

http://www.example.com/location/category/any+keyword

but the problem is my URL keeps changing.

  • If a user enters only a keyword then the URL will be like http://www.example.com/any+keyword and I only have to search in the title and description.

  • If a user enters just a category, the URL will be like http://www.example.com/category and I only have to search in the category.

  • If a user enters a location and keyword, the URL will be like http://www.example.com/location/any+keyword and I only have to search in the location, title and description and vice versa.

... any help will be appreciated.

4
  • How would you think a problem like this could be resolved? – MrWhite Aug 16 '18 at 13:38
  • Why would you even want to do the conversion in that direction? – RalfFriedl Aug 16 '18 at 19:37
  • for user friendly url. A Url like example.com/location/location/category/category/keyword/… its not seem to be good. – livian shrawnia Aug 17 '18 at 4:01
  • thats why i asked a question here MrWhite. if thats not possible then how the www.olx.in do that they r also doing search GET url from htaccess file – livian shrawnia Aug 17 '18 at 4:07
1

You could maybe do something like this if there is really "+" sign in keyword parameter. You always need to have some unique patterns:

RewriteRule    ^(.+\+.+)/?$              index.php?keyword=$1                           [NC,L]
RewriteRule    ^(.+)/?$                  index.php?category=$1                          [NC,L]
RewriteRule    ^(.+)/(.+\+.+)/?$         index.php?category=$1&keyword=$2               [NC,L]
RewriteRule    ^(.+)/(.+)/(.+\+.+)/?$    index.php?location=$1&category=$2&keyword=$3   [NC,L]

If there is not "+" sign every time and it could be just a regular string, so it wouldn't be possible, because keyword and category would have the same regexp pattern.

2
  • then how www.olx.in do that in GET method. i think they r also using htaccess rule to clean url. if thats not possible then please help me atleast with php how can i accomplish this task – livian shrawnia Aug 17 '18 at 4:12
  • Can you send more realistic examples of GET request URLs? I don't know www.olx.in, but there must be more be of course a solution. They can, for example check every request against the database (in production against elasticsearch, solr, memcached, etc. rather then just RDB) for existence of categories, locations and keywords. But of course only they are mutualy exclusive (there wont be location with same name as a category or keyword is). – patok Aug 17 '18 at 8:21
0

By the sounds of it, the URL could consist of any combination of "location", "category" and/or "any+keyword"? (Although your example does not explicitly show that "category" and "any+keyword" would be used together? This would reduce the ambiguity a bit.)

Unless there is a discernable difference between the "location", "category" (and "any+keyword") values, or the number of different locations and categories are sufficiently small for you to be able to actually list (hardcode) them in your directive then there is no way to handle these URL differences in .htaccess alone. They are ambiguous.

If the possible "location" and "category" values are unique then you could perhaps rewrite the URL to your backend application and your application performs the necessary lookups to determine whether it's a "location", "category" or not (ie. a "keyword"). But this potentially involves a lot more work (work as in computing power) and is not scalable, so it not recommended.

Realistically, given your example URL, the only potentially valid URLs that could be derived from this would be:

  • http://www.example.com/location
  • http://www.example.com/location/category
  • http://www.example.com/location/category/any+keyword

but the problem is my url is keep changing

Changing in your application? Or inbound links? If the former then you need to resolve the ambiguity in your application and create a consistent URL structure.

1
  • i am changing url in my application – livian shrawnia Aug 17 '18 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.