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?

图片转代码服务由CSDN问答提供 功能建议

在我的htaccess中,我为我的搜索页面重写规则以创建干净的URL; \ n

原始网址:

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

RewriteRule:

  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.这是有道理的,因为字母ğ和ş是外国字母。 它们必须被编码。 但如果我在原始网址中写下该关键字,

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

它有效。 我得到了结果。 此外,我试图编码用户输入搜索干净URL的内容

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

不起作用。 我得到404.我检查了console.log,用户输入被编码。 因此,我认为rewriterule会导致编码问题。 所以我试图添加一些与编码相关的标志。

  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] 
   
 
 

不起作用。 有什么建议吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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 %&*()#!@

    点赞 打赏 评论

相关推荐 更多相似问题