简体   繁体   中英

How to manipulate the URL from example.com/search/?s=some to example.com/search/some

I have a search form on my website. When submitting the form, it is processed by my index.php, so the URL would be like index.php?s=some. Now I added a few rewrite rules in my htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/$ index.php?s=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?s=$2&f=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)?$ index.php?s=$2&f=$1&t=$3 [QSA,L]

My form has the action "search", it looks like this:

<form id="searchform" method="get" action="search">

Now what happens when submitting is that you get redirected to example.com/search/?s=some But what I want is to be redirected to example.com/search/some What could I add to my rewrite rules to achieve this?

I came across http://matthewjamestaylor.com/blog/how-to-post-forms-to-clean-rewritten-urls when I was looking for a solution for a project of mine. The PHP involved can be a little overkill, but it does have a lot of utility.

.htaccess

# Clean up search URL
RewriteRule ^search/([^/\.]+)$ search.php?q=$1

PHP

<?
    // Collect the search phrase from the URL
    $qs = mysql_real_escape_string($_GET['q']);

    // Clean up by removing unwanted characters
    $qsclean = ereg_replace("[^ 0-9a-zA-Z]", " ", $qs);

    // Remove multiple adjacent spaces
    while (strstr($qsclean, "  ")) {
       $qsclean = str_replace("  ", " ", $qsclean);
    }

    // Prefix each keyword with a + (for the SQL statement)
    $qsql = "+".str_replace(" ", " +", $qsclean);
?>

Why dont you use the post method ? I thing this method make more complex programming Please use the post method. it is more secure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM