简体   繁体   English

PHP .htaccess重写搜索词

[英]PHP .htaccess Rewrite search term

I must to rewrite my search term using .htaccess and instead pass the correct action to my form. 我必须使用.htaccess重写搜索词,然后将正确的操作传递给表单。

<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 因此,我必须传递$src变量以在MySQL查询中提供服务

When i reach the page www.domain.com/search I got an error of undefinied index even if I append ?src=xxx 当我到达www.domain.com/search页面时,即使附加了?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 您需要使用正则表达式捕获组来捕获/ search后的部分,然后在目标URL中将其用作$ 1

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

This will rewrite 这将重写

  • /search/foo / search / foo

to

  • /views/search.php?src=foo /views/search.php?src=foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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