简体   繁体   中英

PHP .htaccess Rewrite search term

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

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