【问题标题】:Removing special characters from query string in .htaccess从 .htaccess 中的查询字符串中删除特殊字符
【发布时间】:2014-10-17 13:44:05
【问题描述】:

我遇到过类似的问题,例如this one,并在mod_rewrite tutorials 上找到了类似的说明。

我已经确定我需要一些类似的东西

RewriteRule ^(.*)<(.*)$ /$1$2 [L,R=301]
RewriteRule ^(.*)>(.*)$ /$1$2 [L,R=301]

这适用于http://domain.com/&lt;&gt;,但它确实适用于http://domain.com?a=&lt;&gt;

在尝试从查询字符串中删除这些字符时,我还添加了以下内容:

RewriteCond %{QUERY_STRING} ^(.*)<(.*)$
RewriteRule ^(.*)$ /$1?%1%2 [L,R=301]
RewriteCond %{QUERY_STRING} ^(.*)>(.*)$
RewriteRule ^(.*)$ /$1?%1%2 [L,R=301]

这并没有改变任何东西。我也尝试在正则表达式中转义 (即^(.*)\&lt;(.*)$)。

我想要达到的最终结果是拥有

http://domain.com/&lt;whatever&gt; 变成http://domain.com/whatever,然后

http://domain.com/a=&lt;whatever&gt;&amp;b=whatever变成http://domain.com/a=whatever&amp;b=whatever

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    &lt; 被浏览器编码为%3C&gt; 被编码为%3E。所以你的规则是这样的:

    RewriteCond %{QUERY_STRING} ^(.*?)(?:%3C|%3E)(.*)$
    RewriteRule ^(.*)$ /$1?%1%2 [L,R=302,NE]
    

    这会将http://domain.com/?a=&lt;whatever&gt;&amp;b=whatever 重定向到http://domain.com/?a=whatever&amp;b=whatever

    【讨论】:

    • 我可以发誓编码发生在 考虑到 .htaccess 之后。很高兴知道 RewriteCond 不是这种情况(或者只是查询字符串?)
    • 是的,我相信它只适用于查询字符串。
    • 完美答案!今天学到了一些关于查询字符串编码时间的新东西。谢谢!
    猜你喜欢
    • 2011-04-11
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    相关资源
    最近更新 更多