Clean URLs for search query?
Asked Answered
F

6

6

This works:

HTML

<a href="/search/querystring">query</a>

htaccess

RewriteRule ^search/([-0-9a-z]+)$ /search.php?q=$1 [L]

Going through a search form:

<form method="get" action="/search">
<input type="search" name="q" value="querystring" />
<input type="submit" />
</form>

Is this possible with htaccess or do I need to redirect with PHP from within search.php?

Example desired result in action: http://twitter.com/search/hello

EDIT

I prefer not to be dependant on JavaScript to do this so search engines and folks with JavaScript disabled will see this too.

Folia answered 28/3, 2011 at 20:6 Comment(0)
A
7

I think the problem is that you've created an HTML form with GET method, which automatically opens the URL that way you specified as the result. If you want to submit your search query like the desired one, you should hack the form with some JavaScript to call your good-looking URL like this:

<form method="get" action="/search/" onsubmit="return false;">
<input type="search" name="q" value="querystring" />
<input type="submit" onclick="window.location.href=this.form.action + this.form.q.value;" />
</form>
Agitprop answered 28/3, 2011 at 20:15 Comment(6)
I cannot think of another solution which does not involve JavaScript, although it has to work in case when a user has his/her JS turned off, just with the other ("ugly") URL.Agitprop
mm. It looks like it's not as straight forward as I pictured it to be.Folia
Yeah, unfortunately it's not possible without JS according to my experience. Isn't falling back to the regular URL on switched-off JavaScript a viable method?Agitprop
Works after some adaptation and modification. Maybe I will find something else eventually but this is a good solution! Going to use it.Folia
Hey folks. How to do that without submit button? I have only form and input?Rim
Ok. I have it: Add this to form -> onsubmit="window.location.href=$('form').attr('action') + $('input').val(); return false;"Rim
I
2

I think you'll actually have to create a separate rewrite rule that is essentially your rewrite rule above but in reverse. Then place it above your first rewrite rule.

RewriteRule ^/search?q=([-0-9a-z]+)$ /search/$1 [NC]
RewriteRule ^search/([-0-9a-z]+)$ /search.php?q=$1 [L]

Seem pretty ghetto to me though. Maybe you should remove the submit button from your form and redirect using javascript.

Incognizant answered 28/3, 2011 at 20:12 Comment(0)
F
2

See the trick on this this page. The trick is to send the form to self (or I guess you could redirect to an intermediate page), and use server side logic to redirect using a clean URL.

Fiberglass answered 7/2, 2013 at 16:27 Comment(2)
Awesome! Just awesome! All you do is post, then have the server redirect to a GET! Nice! Pretty URL's and back button niceness. :)Hogarth
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.Engobe
F
0

For everybody trying to solve this problem only with mod_rewrite (without JavaScript), see this question: Redirect and rewrite with mod_rewrite

Folia answered 29/3, 2011 at 15:21 Comment(0)
L
0

EXAMPLE WITHOUT JAVASCRIPT

Have your search form action set to 'searchredirect.php' instead and input name to 'q'.

Create a new php file called searchredirect.php and have only the following code:

<?php  
if (isset($_GET['q'])){  
$url = $_GET['q'];  
header("location: search/".$url);  
} else {  
header("location: search");
} 
?>

Name you original search page 'searchclean.php'

In your .htaccess file have the following rewrite rule:

RewriteRule ^search/([^/]*)$ /searchclean.php?q=$1 [L]
Lander answered 1/1, 2014 at 7:6 Comment(0)
C
0

I just kept

RewriteRule ^search/([^/\.]+)/?$ search.php?q=$1 [L]

in my .htaccess, i made search_redirect.php with the code given here below:

<?php  
if (isset($_GET['q'])){  
$url = $_GET['q'];  
header("location: search/".$url);  
} else {  
header("location: search");
} 
?>

Then of course I redirected my forms to goto search_redirect.php and voila. This combination worked like a charm, then since I have pagination I didn't have to do anything more, since all of them use GET on the query string and then $i for the integer.

RewriteRule ^search/([^/\.]+)/page/([^/\.]+)?$ search.php?q=$1&pn=$2 [L]

Cordage answered 25/10, 2015 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.