htaccess搜索并用破折号替换加号

时间:2014-04-22 17:29:56

标签: .htaccess url-rewriting

我有两个参数city的表单,关键字传递给search.php,提交后重定向到url之类的 的 website.com/search/lenovo+laptop+dealers/Delhi

但是我需要在没有这些加号的情况下解析url,但在url中使用短划线

示例: website.com/search/lenovo-laptop-dealers/Delhi

我的htaccess文件中的代码

RewriteEngine on
RewriteBase /


RewriteCond %{THE_REQUEST} \s/+search\.php\?keyword=([^&]+)&city=([^\s&]+) [NC]
RewriteRule ^ /search/%1/%2? [R=301,L,NE]

RewriteRule ^search/([\w-]+)/([\w-]+)/?$ /search.php?keyword=$1&city=$2 [L,QSA]

1 个答案:

答案 0 :(得分:0)

您可以遵守以下规则:

RewriteEngine on
RewriteBase /

RewriteRule "^(search)/([^+]*)\++([^+]*\+.*)$" /$1/$2-$3 [L,NC]
RewriteRule "^(search)/([^+]*)\+([^+]*)$" /$1/$2-$3 [L,R=301,NE,NC]

RewriteCond %{THE_REQUEST} \s/+search\.php\?keyword=([^&]+)&city=([^\s&]+) [NC]
RewriteRule ^ /search/%1/%2? [R=301,L,NE]

RewriteRule ^search/([\w-]+)/([\w-]+)/?$ /search.php?keyword=$1&city=$2 [L,QSA]