去掉 ? 来自表单帖子的查询字符串

I currently have a url like: http://testsite.local/search/?q=findme for my search page once someone makes a query. I use mod_rewrite to rewrite some of the pages and was wondering if it was possible to make this into a nice url like: http://testsite.local/search/1/findme/ (the number 1 being the page of the set of results).

Thanks

Ok, none of these methods were what I was looking for, after a little research I just decided to redirect to the correct page when a page has a query string. So when a search was made, I just did this:

if ($_GET["posted"] == "true") {<br />
    header("Location: /search/" . $_GET["q"] . "/1/");<br />
}

I know the page has just been posted from a form as it has the hidden "posted" field.

Use parse_url() and parse_str().

<?php
$url = 'http://testsite.local/search/?q=findme';
$parsed = parse_url($url);
var_dump($parsed);

parse_str($parsed['query'], $params);
var_dump($params);

Output:

array
  'scheme' => string 'http' (length=4)
  'host' => string 'testsite.local' (length=14)
  'path' => string '/search/' (length=8)
  'query' => string 'q=findme' (length=8)
array
  'q' => string 'findme' (length=6)

Use .htaccess file:

RewriteEngine On
RewriteBase /search/

#rewriting only if the request file doesn't exists (you don't want to rewrite request for images or other existing files)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)? index.php?path=$1 [L]

After that, you can use the REQUEST_URI in your root index.php file, and can parse the string in it:

var_dump($_SERVER['REQUEST_URI']);

or you can use $_GET['path']:

This should do the trick:

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

Here is a page that has a little info, Google knows much about this too.

You can swap $1 and $2 if you want to change the order.

edit: If you have something like this in your HTML:

<form id="search_form" method="get" action="search">
  <input type="text" name="q" />
  <input type="submit" value="Search" />
</form>

With jQuery you can do something like this:

$(document).ready(function() {
  $("#search_form").submit(function(event) {
    event.preventDefault();
    window.location = "/search/1/" + escape($(this).find("input[name=q]").val())
  });
});

This will intercept the form submit and do the custom url, and if the user has no javascript, it will still work the old way.