【问题标题】:RewriteRule prevents encodingRewriteRule 防止编码
【发布时间】: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


    【解决方案1】:

    这不起作用,因为您使用([A-Za-z0-9-]+) 仅定义了 alphanumeric characters

    尝试使用([^/]+) 如果你想允许一切

    或使用 ([\p{L}0-9]+) 只允许 unicode 字母和数字,

    但它不允许像%&*()#!@这样的特殊标记

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 2020-01-04
      • 2013-07-28
      相关资源
      最近更新 更多