doukesou4452 2017-11-03 23:40
浏览 24
已采纳

RewriteRule阻止编码

In my htaccess, I have rewrite rule for my search page to create clean URL;

Original URL :

http://www.example.com/index.php?keyword=apple

RewriteRule :

RewriteRule ^keyword/([A-Za-z0-9-]+)/?$ index.php?keyword=$1 

Clean URL :

http://www.example.com/keyword/apple

That works perfectly unless I type foreign characters. For example, If I write

http://www.example.com/keyword/değişmek

I get 404. That makes sense because the letter ğ and ş are foreign letters. They have to be encoded. But if I write that keyword in original URL,

http://www.example.com/index.php?keyword=değişmek

It works. I get results. Also, I tried to encode what users type in input search for clean URL

var url = encodeURIComponent($("#search-input").val());
window.location.href = "http://www.example.com/keyword/"+ url ; 
console.log(url);

didn't work. I get 404. I checked the console.log and the user input is encoded. Therefore, I think rewriterule causes encoding problem. So I have tried to add some flags related to encoding.

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]

didn't work. Any recommendation?

  • 写回答

1条回答 默认 最新

  • dongtan6543 2017-11-04 00:41
    关注

    It doesn't work because you defined only alphanumeric characters using ([A-Za-z0-9-]+),

    Try using ([^/]+) instead If you want to allow everything

    or use ([\p{L}0-9]+) to allow only unicode letters and numbers,

    But it will not allow special marks like %&*()#!@

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料