.htaccess RewriteRule with form parameter

时间:2014-01-14 13:43:29

标签: php apache .htaccess mod-rewrite rewrite

www.example.com / Search.php?q=mySearchString这是我的搜索链接。我想把它改成

www.example.com / Search/mySearchString

然后我在我的.htaccess文件中尝试了以下规则,但它看起来像 - > Search/?q=mySearchString 我如何从地址中删除?q=

在.htaccess文件中我添加了这样的规则;

RewriteRule ^Search/(.*)$ Search.php?q=$1 [L,NC]

我也用表格发送搜索查询,表格的方法是'GET'

 <form class="navbar-search pull-left" method="GET" action="Search/">
    <input name="q" type="text" />
 </form>

我该如何解决?

1 个答案:

答案 0 :(得分:1)

如果您将搜索输入重命名为:

<input name="query" type="text" />

然后使用这两个规则:

# If someone searches /Search/?query=search+string
RewriteCond %{QUERY_STRING} query=([^&]+)
# Redirect to /Search/search+string
RewriteRule ^Search/$ Search/%1 [L,R=301,NC]

# Match the redirect from the rule above and serve up results
RewriteRule ^Search/(.*)$ Search.php?q=$1 [L,NC]