Welcome! Log In Create A New Profile

Advanced

rewrite rules encoding -> how to pass seo friendly search terms?

Posted by Kleverhans 
rewrite rules encoding -> how to pass seo friendly search terms?
June 03, 2010 03:18PM
Hello everybody,

after searching the web for several hours to no result I hope my issue can be solved here.

We run a website www.example.com and want to have seo friendly search links such as www.example.com/search/hello+world

These should be rewritten to www.example.com/search.php?q=hello+world - we do this with this rewrite rule:
rewrite ^/search/(.*)$ /search.php?q=$1

This obviously works, only nginx seems not to decode the "+" sign (which is an urlencoded space character) but decodes all other urlencoded characters (%xx). So, for the given example $_GET['q'] in php does not contain "hello world" but "hello+world". This could of course be fixed by replacing the "+" with " " in php.

Now, if the search string also contains some encoded + sign (%2B, such as hello+world%2Bfriends) this results in $_GET['q'] to contain "hello+world+friends", so it is not possible to figure out what is an actual + and what was a space character. To give a more real world example, the search term could be "c++ book", which urlencoded is "c%2B%2B+book" but results in php $_GET['q'] as "c+++book".

Probably I miss something here, what setting/rewrite rule is needed to solve this? Shouldn't nginx decode "+" as well or pass the whole string unencoded (which seems not possible because it needs to decode for the regex to work).

Hope anybody can point me in the right direction, I would assume this is a quite common use of rewrite rules to make such links more seo friendly and human readable.

Thanks - Ralf

Edit: I know space can also be encoded as %20, but e.g. the firefox search bar plugin does it as "+", so it is not a matter of fixing it on the website only.



Edited 1 time(s). Last edit at 06/03/2010 03:19PM by Kleverhans.
Re: rewrite rules encoding -> how to pass seo friendly search terms?
June 04, 2010 09:24AM
Just in case anybody ever comes across this issue here is how we kind of solved it now:

We simply do no longer rewrite in nginx passing the value as q= to the php script (as shown above), but parse the request_uri directly in search.php using

[code]
$searchkeyword = urldecode(trim(preg_replace('/\/search\//i', '', $_SERVER['REQUEST_URI'], 1)));
[/code]

Of course, if any other solution to the issue comes to mind feel free to post and correct.

Cheers, Ralf
Re: rewrite rules encoding -> how to pass seo friendly search terms?
January 27, 2011 04:14PM
I ran into a similar problem. It would be nice to have the option to have the url passed in Nginx through the rewrite without encoding the url. In other words, have a decode flag inside the Nginx config file. I chose to solve my problem by decoding the url within php. I still use a rewrite as follows:

[code]
rewrite ^/search/(.*) /index.php?cmd=search&terms=$1;
[/code]

And then in my search.php file I simply pass the search terms through the urldecode() function in php.

[code]
urldecode($request->getHTTPproperty('terms'))
[/code]

All my problems are solved. :D
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 259
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready