开发者

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

开发者 https://www.devze.com 2023-04-05 08:30 出处:网络
I\'m using jQuery Mobile and its mostly good. Although there are a few errors. For example; I put in my HTML & instead of & like you\'re meant to. jQuery doesn\'t read it properly and whe

I'm using jQuery Mobile and its mostly good. Although there are a few errors.

For example; I put in my HTML & instead of & like you're meant to. jQuery doesn't read it properly and when it shows it in the url has & showing instead of & like its meant to.

For example

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

It will go to

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

when really it should go to

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

It's really annoying and I use lots of them so I cannot manally write a .htaccess because all the get variables change and it would be a very long file it I did write every possible foo and bar.

My question is: Is the开发者_开发知识库re a quick way for an apache server to correct &amp; to &?


The string index.php?foo=true&amp;bar=false, when used as an attribute's value, is decoded as index.php?foo=true&bar=false by the browser.

So, clicking on this link:

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

Goes to index.php?foo=true&bar=false, not index.php?foo=true&amp;bar=false.

This is because & is a special character in HTML, and &amp; is how you can encoded it to remove its special meaning.

It's actually invalid to write <a href="index.php?foo=true&bar=false">, and perfectly valid to write <a href="index.php?foo=true&amp;bar=false">.


If jQuery reads it wrongly, there must be a bug in jQuery, or you are doing something wrong.

If you have tons users of using doing requests to index.php?foo=true&amp;bar=false, you may want to rewrite the request like this:

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

(Using RewriteCond and matching on %{THE_REQUEST} because a RewriteRule won't match on the query string.)

This will redirect the user to the URL with &amp; decoded to &. If there is multiple &amp;, there will be multiple redirects.

0

精彩评论

暂无评论...
验证码 换一张
取 消