用于重写GET URL的.htaccess规则

时间:2010-08-03 20:40:07

标签: apache mod-rewrite

在网站上,我有一个方法获取的搜索表单。我想知道我是否可以将该网址重写为更好看的网址。

例如,现在我提交搜索表单后,我得到一个这样的网址:

  

http://www.mywebsite.com/search.php?search_category=0&search_term=Jackson&submit_search=

如果我能看起来像这样会很棒:

  

http://www.mywebsite.com/search/Jackson/

感谢。

2 个答案:

答案 0 :(得分:4)

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^/search/(.*)/ /search.php?search_category=0&search_term=$1&submit_search= [L]

答案 1 :(得分:0)

您需要进行两次重定向,因为您可能希望后者也被认为代表前者:

# /search.php?search_term=foo to /search/foo/
RewriteCond %{THE_REQUEST} ^GET\ /search\.php\?([^& ]*&)*search_term=([^& ]*)
RewriteRule ^/search\.php$ /search/%2/ [L,R=301]

# /search/foo/ to /search.php?search_term=foo
RewriteRule ^/search/([^/]+)/$ search.php?search_category=0&search_term=$1&submit_search [L]