Rewrite search.php to /search/

I have a search form in page ‘search.php’ and I’m trying to write this rule:

RewriteRule ^search/(.*)$ search.php?q=$1

This does not work, as the query string is appended to the URL like:

http://example.com/search/?q=something

I guess I need to write a RewriteCond with {QUERY_STRING} etc. but how? Just couldn’t figure it out…

Beau,

That’s what I was suggesting above. Too reasonable, eh?

GG,

WHERE did I miss your NE flag (or lack thereof)?

Loopy? They guy who wrote that … well, I’ll leave that alone except to tell you that you can stop loopy code like that with the NS flag.

Regards,

DK

Reading about it a little more, I realized that we also need a redirect here.

I had solved this same issue with a PHP redirect once and it seems we still need one in mod_rewrite.

More or less what I need, I’ve got something like:


<form action="searchrd">
<input name="q" type="text" value="" />
</form>


RewriteCond %{REQUEST_URI} searchrd$
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^(.*)$ /search/%1? [R=301,L]

RewriteRule ^search/(.*)$ search.php?q=$1

Based on: http://www.talkincode.com/using-mod_rewrite-on-form-parameters-89.html

Unfortunately I’ll go back to PHP redirect method, as an .htaccess redirect encodes utf-8 characters in the URL which does not look good in the browser’s address bar.

Any other thoughts and advices about this matter still appreciated :slight_smile:

Well I found a way to unescape the utf-8 characters in the query string with RewriteMap directive, but it seems that RewriteMap is not allowed in .htaccess unfortunately.

So I still have the PHP redirect based solution which would use more resources :frowning:

gg,

Hmmm, your rewrite code looks okay to me but your form isn’t directing to search?q={whatever}. What’s up with that?

I’m sure you’ve been through this before but YOU create the “new format” links and then YOU create the mod_rewrite code to redirect that to the “serve-able” script. The form redirects (with a valid action), to search.php?q={whatever}.

Regards,

DK

David,

Do you mean why it’s “searchrd” instead of “search” in the form action?

Anyway, I won’t be able to use this method until I find a way to unescape utf-8 chars in the query string.

gg,

Almost. I meant that action of searchrd means NOTHING and does not match your regex anyway. Basically, using a form, you’re stuck with the search.php?q={whatever} which will automatically escape characters in the {whatever} value.

Regards,

DK

Yes, but when I do not use htaccess redirect, escaped utf-8 chars are displayed as letters in the address bar of modern browsers. On the other hand the mod_rewrite-escaped chars are not displayed as letters.

By the way do you mind writing the correct html and the rewrite code. I’d read something about an endless loop in the link I mentioned above. That’s why I changed it to ‘searchrd’ while testing.

I can’t see why it does not match the regex above. This is not the exact code I used but as far as I remember it worked.

You should post the snippets of exact code that you’re using. It’s hard to tell exactly what you’re trying to do. Are you sure you don’t want to just POST the data from the form?

Okay, in order to help me and others that might read this thread, would any of you write a sample code for rewriting:

search.php?q={query}
to
/search/{query}

David thanks for the flags notice.

I think this level of rewriting is over my head right now and I have other things to take care of in the project so I’m now settled with a simpler rewrite without a redirect.

gg,

Because it’s inadvisable to write “loopy” code, i.e., redirect FROM the serve-able code to a new format and then back to the serve-able code is a clear abuse of the server, let me decline. IF you are interested, I do have this covered in my tutorial along with a WARNING NOT to do this on a production server - especially a server which is not dedicated and which you mod_rewrite code is residing in an .htaccess file.

Regards,

DK

Thanks, I’ll read your article now. (Oh I realized I’ve already read some parts of it before).

Is there an advisable method that we can use to have our search result page URLs in:

/search/{query}

format?

gg,

Hmmm, other than mod_rewrite, I can only think of hijacking the Submit function with jQuery and using it to send the request in the format you’d like to see.

IF you insist on using mod_rewrite, the following SHOULD do it for you (but please report back any problems so others don’t encounter the same difficulties):

RewriteEngine on
RewriteCond %{QUERY_STRING} q=([^&]+)
RewriteRule ^search\\.php$ search/%1 [R=301,NS]
RewriteRule ^search/(.+)$ search.php?q=$1 [L]

The R=301 should send a redirection to the browser’s location box and the NS (no subrequest) should prevent looping.

should = UNTESTED.

Regards,

DK

David,

Thanks for valuable contribution to this thread. Below is my current rewrite choice (hoping it’s in good shape), but I’ll test the code you’ve provided when I have time.


RewriteCond %{REQUEST_URI} /something/search$
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^(.*)$ /something/search.php?q=%1