PHP .htaccess重写搜索词

I must to rewrite my search term using .htaccess and instead pass the correct action to my form.

<form method="GET" action="/search/">
  <input type="text" name="src"/>
  <button>Search</button> 
</form>

I'm using this rule to rewrite the search URL:

RewriteRule ^search/?$ views/search.php?src=$1 [NC,QSA,L]

So I must to pass the $src variable to serve in the MySQL query

When i reach the page www.domain.com/search I got an error of undefinied index even if I append ?src=xxx

Here is result page code (some parts are cut):

$term = $_GET['src'];

if(isset($term) !== NULL)
    {  
    $resultsrc = $mysqli -> query("SELECT * FROM `products` WHERE name LIKE '" . $term . "' OR brand LIKE '" . $term . "' LIMIT 0 , 30");
}
    while($row = mysqli_fetch_all($resulsrc))
   { 
 [... etc ... ]
  }
}

Thanks to all who can help.

You need to use a regex capture group to capture the part after /search in url and then use it in the target url as $1

RewriteRule ^search/(.*)/?$ views/search.php?src=$1 [NC,QSA,L]

This will rewrite

  • /search/foo

to

  • /views/search.php?src=foo