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.

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
DADU
  • 6,482
  • 7
  • 42
  • 64

6 Answers6

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>
Imi Borbas
  • 3,683
  • 1
  • 19
  • 16
  • 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. – Imi Borbas Mar 28 '11 at 20:34
  • mm. It looks like it's not as straight forward as I pictured it to be. – DADU Mar 28 '11 at 20:43
  • 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? – Imi Borbas Mar 28 '11 at 20:51
  • Works after some adaptation and modification. Maybe I will find something else eventually but this is a good solution! Going to use it. – DADU Mar 28 '11 at 22:16
  • Hey folks. How to do that without `submit button`? I have only `form` and `input`? – Maciej A. Czyzewski Aug 01 '13 at 09:47
  • Ok. I have it: Add this to `form` -> `onsubmit="window.location.href=$('form').attr('action') + $('input').val(); return false;"` – Maciej A. Czyzewski Aug 01 '13 at 09:58
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.

paul B
  • 206
  • 3
  • 12
  • Awesome! Just awesome! All you do is post, then have the server redirect to a GET! Nice! Pretty URL's and back button niceness. :) – gregthegeek May 21 '13 at 02:53
  • 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. – Gerald Schneider Sep 17 '14 at 12:25
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.

vicTROLLA
  • 1,554
  • 12
  • 15
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]
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]

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Steini Petur
  • 129
  • 1
  • 9
0

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

Community
  • 1
  • 1
DADU
  • 6,482
  • 7
  • 42
  • 64