【发布时间】:2017-11-04 09:11:01
【问题描述】:
在我的 htaccess 中,我为我的搜索页面重写规则以创建干净的 URL;
原始网址:
http://www.example.com/index.php?keyword=apple
重写规则:
RewriteRule ^keyword/([A-Za-z0-9-]+)/?$ index.php?keyword=$1
干净的网址:
http://www.example.com/keyword/apple
除非我输入外来字符,否则效果很好。例如,如果我写
http://www.example.com/keyword/değişmek
我得到 404。这是有道理的,因为字母 ğ 和 ş 是外来字母。它们必须被编码。但是,如果我在原始 URL 中写入该关键字,
http://www.example.com/index.php?keyword=değişmek
它有效。我得到结果。另外,我尝试对用户在输入搜索中输入的内容进行编码以搜索干净的 URL
var url = encodeURIComponent($("#search-input").val());
window.location.href = "http://www.example.com/keyword/"+ url ;
console.log(url);
没用。我得到 404。我检查了 console.log 并且用户输入被编码。因此,我认为重写会导致编码问题。所以我尝试添加一些与编码相关的标志。
RewriteRule ^keyword/([A-Za-z0-9-]+)/?$ index.php?keyword=$1 [NE]
RewriteRule ^keyword/([A-Za-z0-9-]+)/?$ index.php?keyword=$1 [B]
RewriteRule ^keyword/([A-Za-z0-9-]+)/?$ index.php?keyword=$1 [NE,B]
没用。有什么推荐吗?
【问题讨论】:
标签: php jquery .htaccess mod-rewrite encoding