Php 在没有查询字符串的情况下,如何将搜索表单生成的查询字符串转换为新URL?
我有一个搜索表单,在提交时生成如下url:Php 在没有查询字符串的情况下,如何将搜索表单生成的查询字符串转换为新URL?,php,.htaccess,search,redirect,query-string,Php,.htaccess,Search,Redirect,Query String,我有一个搜索表单,在提交时生成如下url: http://mydomain.com/index.php?find=some+text 我的目标是让它看起来像: http://mydomain.com/find/some+text 如何使用.htaccess执行此操作 到目前为止,我有: RewriteCond %{REQUEST_URI} ^/index\.php$ RewriteCond %{QUERY_STRING} &?find=(.*)&? RewriteRule
http://mydomain.com/index.php?find=some+text
我的目标是让它看起来像:
http://mydomain.com/find/some+text
如何使用.htaccess执行此操作
到目前为止,我有:
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} &?find=(.*)&?
RewriteRule ^index.php$ find/%1? [R=301,L]
RewriteRule ^find/(.*)$ index.php?find=$1 [L]
如果查询(即我搜索的内容)仅包含数字、字母或下划线,但我希望使其能够搜索任何内容,包括空格和其他字符,则此操作有效 这样的事情不应该奏效吗
RewriteRule ^find/([^/]*)$ /index.php?find=$1 [L]
您需要匹配实际请求,而不是URI,因为URI是通过第二条规则重写的。这会导致重定向循环 要重定向浏览器,请执行以下操作:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?find=([^&\.]+)(&([^\ ]+))?
RewriteRule ^/?index.php$ /find/%1?%3 [L,R=301]
在内部重写它
RewriteRule ^/?find/(.*)$ /index.php?find=$1 [L,QSA]
因此,404错误可能是由于web服务器中的某些配置不允许URL具有+(加号)。 它将在第一个+处中断,并尝试查找该名称的文件 整理好后,重写规则如下:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /*.index\.php\?find=([^&\.]+)?\sHTTP
RewriteRule ^/?index.php$ /find/%1? [L,R=301]
RewriteRule ^/?find/(.*)$ /index.php?find=$1 [L,NC]
谢谢你的帮助 这看起来可能会让你陷入困境?嗨!谢谢你的回复。例如,如果查询变量find有一个空格,它将不起作用。而且也只有当我自己输入find/sometext或在链接中输入,并且我需要它来自表单提交时才有效。谢谢您的回复!哼我不知道这是否是问题所在,但这是有道理的。即使如此,对于这个版本,我仍然无法使用多个单词进行搜索,因为它将返回404。