Jump to content

URL redirection problem


Fmdpa

Recommended Posts

I'm building a search engine feature on my website right now, and I want the search urls to be rewritten as /search/mode/query.

RewriteRule ^/?search\.php?query=(.*?)&mode=(.*?)/?$ search/$1/$2 [R]

The redirection works fine until I get to the equals symbol. Even when I try escaping it, the redirection fails. What's wrong?.htaccess rewrites are so hard to debug. :)

Link to comment
Share on other sites

I think you're thinking about rewrites the wrong way round - if you want, when someone types search/mode/query, the page at search.php?query=query&mode=mode to be loaded, then you want to rewrite the former to the latter, not the other way round:

RewriteRule ^search/(.*?)/(.*?)$ search.php?query=$1&mode=$2 [R]

P.S. if you really do want to rewrite the querystring, I think you need to use RewriteCond with %{QUERY_STRING}.

Link to comment
Share on other sites

I'm building a search engine feature on my website right now, and I want the search urls to be rewritten as /search/mode/query.
RewriteRule ^/?search\.php?query=(.*?)&mode=(.*?)/?$ search/$1/$2 [R]

The redirection works fine until I get to the equals symbol. Even when I try escaping it, the redirection fails. What's wrong?.htaccess rewrites are so hard to debug. :)

You forgot to escape the query string's question mark. Should be:
RewriteRule ^search\.php\?query=(.*?)&mode=(.*?)/?$ search/$1/$2 [R]

However, as I understand it, the query string is never included in the pattern of a rewriterule, so you'll have to do something like this instead:

RewriteCond %{QUERY_STRING} ^query=(.*?)&mode=(.*?)$ RewriteRule ^search\.php$ /search/%1/%2 [R]

Link to comment
Share on other sites

I think you're thinking about rewrites the wrong way round - if you want, when someone types search/mode/query, the page at search.php?query=query&mode=mode to be loaded, then you want to rewrite the former to the latter, not the other way round
Synook, I thought this at first as well, however, I noticed he was using the [R] option, which forces an external redirection, and I also noticed it was a search, presumably from a regular form, and the uri scheme hes trying to create would be impossible to achieve by submitting a form. So I'm guessing what he's trying to do is to have the user submit the form, and once the form is submitted to "search.php?search=whatever", have that get redirected to a nicer looking uri scheme, like: "/search/whatever".
Link to comment
Share on other sites

You are correct, Dilated. Actually it wouldn't be impossible to create that URI format:

<?phpif (isset($_POST)) {   $query = $_POST['query'];   $mode = $_POST['mode'];   header("Location: /search/$mode/$query");}

Ok, I really don't want to do that every time a search is performed, which is why I'm doing it with .htaccess. :) Dilated, your RewriteCond almost did it. It rewrote everything just as I wanted it, but the query string remained. How can I eliminate the query string?

Link to comment
Share on other sites

You are correct, Dilated. Actually it wouldn't be impossible to create that URI format:
It is impossible to get a uri like that from a basic form without using some kind of workaround, whether it be .htaccess, PHP, or javascript.
Ok, I really don't want to do that every time a search is performed, which is why I'm doing it with .htaccess. :) Dilated, your RewriteCond almost did it. It rewrote everything just as I wanted it, but the query string remained. How can I eliminate the query string?
There shouldn't be a query string left over unless you're using the QSA option (query string append). Are you sure there are only going to be two query string variables from the search?
RewriteCond %{QUERY_STRING} ^query=(.*?)&mode=([^&]*).*?$

Try using that instead to make sure it doesn't grab extra variables that may be in the query string.

Link to comment
Share on other sites

After reading some more mod_rewrite documentation, I found out that you can disable QSA by appending a ? to the replacement pattern:

RewriteCond %{QUERY_STRING} ^query=(.*?)&mode=(.*?)$RewriteRule ^search\.php$ /search/%1/%2? [R]

...which worked!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...