一个 URL 中有多个问号是否有效? [英] Is it valid to have more than one question mark in a URL?

查看:94
本文介绍了一个 URL 中有多个问号是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天遇到了以下网址:

I came across the following URL today:

http://www.sfgate.com/cgi-bin/blogs/inmarin/detail??blogid=122&entry_id=64497

注意查询字符串开头的双问号:

Notice the doubled question mark at the beginning of the query string:

??blogid=122&entry_id=64497

我的浏览器似乎没有任何问题,并且运行了一个快速书签:

My browser didn't seem to have any trouble with it, and running a quick bookmarklet:

javascript:alert(document.location.search);

刚刚给了我上面显示的查询字符串.

just gave me the query string shown above.

这是一个有效的网址吗?我如此迂腐的原因(假设我是)是因为我需要为查询参数解析这样的 URL,并且支持双问号需要对我的代码进行一些更改.显然,如果他们在野外,我需要支持他们;我主要想知道是我没有完全遵守 URL 标准的错,还是它实际上是一个非标准的 URL.

Is this a valid URL? The reason I'm being so pedantic (assuming that I am) is because I need to parse URLs like this for query parameters, and supporting doubled question marks would require some changes to my code. Obviously if they're in the wild, I'll need to support them; I'm mainly curious if it's my fault for not adhering to URL standards exactly, or if it's in fact a non-standard URL.

推荐答案

是的,它是有效的.只有 URL 中的 first ? 有意义,任何后面的都被视为字面问号:

Yes, it is valid. Only the first ? in a URL has significance, any after it are treated as literal question marks:

查询组件由第一个问号(?")字符并以数字结尾符号(#")字符或结尾URI.

The query component is indicated by the first question mark ("?") character and terminated by a number sign ("#") character or by the end of the URI.

...

字符斜线(/")和问号(?")可以代表数据在查询组件中.谨防那些年长的、错误的实现可能无法处理这样的正确使用数据作为相对引用的基本 URI(第 5.1 节),显然是因为它们无法区分查询数据查找时的路径数据分层分隔符.然而,作为查询组件通常用于携带识别信息键=值"的形式一对和一个经常使用的值是一个参考到另一个 URI,有时更好为了避免可用性对这些字符进行百分比编码.

The characters slash ("/") and question mark ("?") may represent data within the query component. Beware that some older, erroneous implementations may not handle such data correctly when it is used as the base URI for relative references (Section 5.1), apparently because they fail to distinguish query data from path data when looking for hierarchical separators. However, as query components are often used to carry identifying information in the form of "key=value" pairs and one frequently used value is a reference to another URI, it is sometimes better for usability to avoid percent-encoding those characters.

https://www.rfc-editor.org/rfc/rfc3986#section-3.4

这篇关于一个 URL 中有多个问号是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆