将index.php?foo=bar&bar=foo重写为index.php?foo=bar&bar=foo


Rewrite index.php?foo=bar&bar=foo to index.php?foo=bar&bar=foo

我正在使用jQuery Mobile,它大部分都很好。虽然有一些错误。

例如

;我把我的HTML &而不是&像你想的那样。jQuery不能正确地读取它,当它在url中显示&而不是&时,就像它的意思一样。

例如

<a href="index.php?foo=true&amp;bar=false">Hello</a>

它会转到

 example.com/index.php?foo=true&amp;bar=false

而实际上它应该去

 example.com/index.php?foo=true&bar=false

这真的很烦人,我用了很多,所以我不能手动写一个。htaccess,因为所有的get变量都变了,它会是一个很长的文件,如果我写了所有可能的foo和bar。

我的问题是:是否有一个快速的方法为apache服务器纠正&amp;& ?

字符串index.php?foo=true&amp;bar=false,当用作属性值时,被浏览器解码为index.php?foo=true&bar=false

那么,点击这个链接:

<a href="index.php?foo=true&amp;bar=false">Hello</a>

转到index.php?foo=true&bar=false,而不是index.php?foo=true&amp;bar=false

这是因为&是HTML中的一个特殊字符,而&amp;是如何对其进行编码以去除其特殊含义的。

实际上写<a href="index.php?foo=true&bar=false">是无效的,而写<a href="index.php?foo=true&amp;bar=false">是完全有效的。


如果jQuery读错了,一定是jQuery有bug,或者是你做错了什么。

如果你有大量的用户使用正在做的请求到index.php?foo=true&amp;bar=false,你可能想重写请求像这样:

RewriteCond %{THE_REQUEST} ^(GET|HEAD) (.*)&amp;(.*) HTTP/['d.]+$
RewriteRule %2&%3 [R]

(使用RewriteCond并匹配%{THE_REQUEST},因为RewriteRule无法匹配查询字符串。)

这将把用户重定向到&amp;解码为&的URL。如果有多个&amp;,则会有多个重定向