query Htaccess RewriteRule ^search/([-0-9a-z]+)$ /search.php?...">

Clear search query urls?

It works:

HTML

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

Htaccess

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

Passing the 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 from PHP from search.php?

An example of the desired result in action: http://twitter.com/search/hello

EDIT

I prefer not to depend on JavaScript for this, so search engines and people with JavaScript disabled will see this too.

+5
php search .htaccess clean-urls
source share
6 answers

I think the problem is that you created an HTML form with a GET method that automatically opens the URL that you specified as the result. If you want to submit your search request like the one you need, you must hack the form using JavaScript to name your beautiful URL as follows:

 <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> 
+7
source share

See the trick on this page . The trick is to submit the form yourself (or, I think you can redirect to an intermediate page) and use server-side logic to redirect using a clean URL.

+2
source share

I think you really need to create a separate rewrite rule, which is essentially your rewrite rule above, but vice versa. Then put it over your first rewrite rule.

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

Looks like a pretty ghetto to me. You may need to remove the submit button from your form and redirect using javascript.

+1
source share

For anyone trying to solve this problem only with mod_rewrite (without JavaScript), see this question: Redirecting and rewriting with mod_rewrite

0
source share

EXAMPLE WITHOUT JAVASCRIPT

You have an action for the search form set instead of 'searchredirect.php' and enter the name in 'q'.

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

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

Name the original search page 'searchclean.php'

Your .htaccess file has the following rewrite rule:

 RewriteRule ^search/([^/]*)$ /searchclean.php?q=$1 [L] 
0
source share

I just held

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

in my .htaccess, I did search_redirect.php with the code 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 no longer needed to do anything, since they all use GET in the query string, and then $ i for an integer.

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

0
source share

All Articles