Php apache重写搜索url
我有一个简单的表单和get方法Php apache重写搜索url,php,apache,.htaccess,url-rewriting,Php,Apache,.htaccess,Url Rewriting,我有一个简单的表单和get方法 <form method="get" action="search.php"> <input type="text" name="category_name" value="cars"> <input type="text" name="location" value="newyork"> </form> 我会制定一个改写规则: http://my-site.com/cars/newyork.html 我已尝试使
<form method="get" action="search.php">
<input type="text" name="category_name" value="cars">
<input type="text" name="location" value="newyork">
</form>
我会制定一个改写规则:
http://my-site.com/cars/newyork.html
我已尝试使用此代码,但不起作用:
RewriteEngine on
RewriteRule (.*)/(.*)\.html$ search.php?category_name=$1&location=$2 [R=301,L]
包括您拥有的规则,请尝试添加以下规则:
RewriteCond %{THE_REQUEST} ^GET\ /search\.php\?category_name=([^&]+)&location=([^&\ ]+)
RewriteRule ^ /%1/%2.html? [L,R=301]
此外,您可能需要为您拥有的规则添加几个条件:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)\.html$ search.php?category_name=$1&location=$2 [R=301,L]
第一个URL的格式是浏览器发送HTML GET表单的格式–您不能更改该服务器端。如何解决?可以通过php重定向吗?一旦请求到达服务器,就可以执行显式重定向。或者,您可以在客户端执行此操作,方法不是以正常方式发送表单,而是使用JS读取值并手动创建URL。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)\.html$ search.php?category_name=$1&location=$2 [R=301,L]