Html URL 是否允许包含空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/497908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is a URL allowed to contain a space?
提问by Joe Casadonte
Is a URI (specifically an HTTP URL) allowed to contain one or more space characters? If a URL mustbe encoded, is +
just a commonly followed convention, or a legitimate alternative?
是否允许 URI(特别是 HTTP URL)包含一个或多个空格字符?如果必须对 URL进行编码,那么它+
只是一个普遍遵循的约定,还是一个合法的替代方案?
In particular, can someone point to an RFC that indicates that a URL with a space mustbe encoded?
特别是,有人可以指向一个 RFC,表明必须对带有空格的 URL进行编码吗?
Motivation for question:While beta-testing a web site, I noted that some URLs were constructed with spaces in them. Firefox seemed to do the right thing, which surprised me! But I wanted to be able to point the developers to an RFC so that they would feel the need to fix those URLs.
问题的动机:在对网站进行 Beta 测试时,我注意到一些 URL 中包含空格。Firefox 似乎做对了,这让我感到惊讶!但我希望能够将开发人员指向 RFC,以便他们觉得需要修复这些 URL。
采纳答案by Marc Novakowski
As per RFC 1738:
根据RFC 1738:
Unsafe:
Characters can be unsafe for a number of reasons. The space character is unsafe because significant spaces may disappear and insignificant spaces may be introduced when URLs are transcribed or typeset or subjected to the treatment of word-processing programs.The characters
"<"
and">"
are unsafe because they are used as the delimiters around URLs in free text; the quote mark ("""
) is used to delimit URLs in some systems. The character"#"
is unsafe and should always be encoded because it is used in World Wide Web and in other systems to delimit a URL from a fragment/anchor identifier that might follow it. The character"%"
is unsafe because it is used for encodings of other characters. Other characters are unsafe because gateways and other transport agents are known to sometimes modify such characters. These characters are"{"
,"}"
,"|"
,"\"
,"^"
,"~"
,"["
,"]"
, and"`"
.All unsafe characters must always be encoded within a URL. For example, the character
"#"
must be encoded within URLs even in systems that do not normally deal with fragment or anchor identifiers, so that if the URL is copied into another system that does use them, it will not be necessary to change the URL encoding.
不安全:
出于多种原因,字符可能不安全。 空格字符是不安全的,因为当 URL 被转录或排版或经受文字处理程序的处理时,重要的空格可能会消失,并且可能会引入不重要的空格。字符
"<"
和">"
是不安全的,因为它们被用作自由文本中 URL 的分隔符;"""
在某些系统中,引号 ( ) 用于分隔 URL。该字符"#"
是不安全的,应该始终进行编码,因为它在万维网和其他系统中用于将 URL 与可能跟随它的片段/锚标识符分隔开。人物"%"
不安全,因为它用于其他字符的编码。其他字符是不安全的,因为网关和其他传输代理有时会修改这些字符。这些字符是"{"
,"}"
,"|"
,"\"
,"^"
,"~"
,"["
,"]"
,和"`"
。所有不安全的字符必须始终在 URL 中编码。例如,
"#"
即使在通常不处理片段或锚标识符的系统中,字符也必须在 URL 中编码,这样如果 URL 被复制到另一个使用它们的系统中,则无需更改 URL 编码。
回答by Julien
Why does it have to be encoded? A request looks like this:
为什么一定要编码?请求如下所示:
GET /url HTTP/1.1
(Ignoring headers)
There are 3 fields separated by a white space. If you put a space in your url:
有 3 个字段由空格分隔。如果您在网址中放置一个空格:
GET /url end_url HTTP/1.1
You know have 4 fields, the HTTP server will tell you it is an invalid request.
你知道有 4 个字段,HTTP 服务器会告诉你这是一个无效的请求。
GET /url%20end_url HTTP/1.1
3 fields => valid
3 个字段 => 有效
Note: in the query string (after ?), a space is usually encoded as a +
注意:在查询字符串中(在?之后),空格通常被编码为+
GET /url?var=foo+bar HTTP/1.1
rather than
而不是
GET /url?var=foo%20bar HTTP/1.1
回答by Peter Hilton
Shorter answer: no, you must encode a space; it iscorrect to encode a space as +
, but only in the query string; in the path you must use %20
.
简短的回答:不,你必须编码一个空格;将空格编码为是正确的+
,但仅限于查询字符串中;在您必须使用的路径中%20
。
回答by Rob Williams
URLs are defined in RFC 3986, though other RFCs are relevant as well but RFC 1738is obsolete.
URL 在RFC 3986中定义,尽管其他 RFC 也相关,但RFC 1738已过时。
They may not have spaces in them, along with many other characters. Since those forbidden characters often need to be represented somehow, there is a scheme for encoding them into a URL by translating them to their ASCII hexadecimal equivalent with a "%" prefix.
它们中可能没有空格以及许多其他字符。由于这些禁用字符通常需要以某种方式表示,因此有一种方案可以通过将它们转换为带有“%”前缀的 ASCII 十六进制等价物来将它们编码为 URL。
Most programming languages/platforms provide functions for encoding and decoding URLs, though they may not properly adhere to the RFC standards. For example, I know that PHP does not.
大多数编程语言/平台都提供了对 URL 进行编码和解码的函数,尽管它们可能不符合 RFC 标准。例如,我知道 PHP 没有。
回答by user54650
Yes, the space is usually encoded to "%20" though. Any parameters that pass to a URL should be encoded, simply for safety reasons.
是的,该空间通常被编码为“%20”。任何传递给 URL 的参数都应该被编码,仅仅是出于安全原因。
回答by A.M Web Surfer
URL can have an Space Character in them and they will be displayed as %20 in most of the browsers, but browser encoding rules change quite often and we cannot depend on how a browser will display the URL.
URL 中可以有一个空格字符,它们在大多数浏览器中将显示为 %20,但浏览器编码规则经常变化,我们不能依赖浏览器将如何显示 URL。
So Instead you can replace the Space Character in the URL with any character that you think shall make the URL More readable and ' Pretty ' ;) ..... O so general characters that are preferred are "-","_","+" .... but these aren't the compulsions so u can use any of the character that is not supposed to be in the URL Already.
因此,您可以将 URL 中的空格字符替换为您认为会使 URL 更具可读性和“漂亮”的任何字符;) ..... O 所以首选的一般字符是“-”、“_”, "+" .... 但这些不是强制性的,所以你可以使用任何不应该出现在 URL 中的字符。
Please avoid the %,&,},{,],[,/,>,< as the URL Space Character Replacement as they can pull up an error on certain browsers and Platforms.
请避免将 %,&,},{,],[,/,>,< 作为 URL 空格字符替换,因为它们会在某些浏览器和平台上引发错误。
As you can see the Stak overflow itself uses the '-' character as Space(%20) replacement.
正如您所看到的,Stak 溢出本身使用“-”字符作为 Space(%20) 替换。
Have an Happy questioning.
有一个快乐的提问。
回答by Julian Reschke
Can someone point to an RFC indicating that a URL with a space must be encoded?
有人可以指向一个 RFC,表明必须对带有空格的 URL 进行编码吗?
URIs, and thus URLs, are defined in RFC 3986.
URI 以及 URL 在 RFC 3986 中定义。
If you look at the grammar defined over there you will eventually note that a space character never can be part of a syntactically legal URL, thus the term "URL with a space" is a contradiction in itself.
如果您查看那里定义的语法,您最终会注意到空格字符永远不能成为句法上合法的 URL 的一部分,因此术语“带空格的 URL”本身就是一个矛盾。
回答by Chris Ballance
Urls should nothave spaces in them. If you need to address one that does, use its encoded value of %20
网址应不会有他们的空间。如果您需要解决一个问题,请使用其编码值%20
回答by Eric Schoonover
To answer your question. I would say it's fairly common for applications to replace spaces in values that will be used in URLs. The reason for this is ussually to avoid the more difficult to read percent (URI) encoding that occurs.
回答你的问题。我会说应用程序替换 URL 中将使用的值中的空格是相当普遍的。这样做的原因通常是为了避免出现更难阅读的百分比 (URI) 编码。
Check out this wikipedia article about Percent-encoding.
查看这篇关于Percent-encoding 的维基百科文章。
回答by Sophie Alpert
Firefox 3 will display %20
s in URLs as spaces in the address bar.
Firefox 3 会将%20
URL 中的 s显示为地址栏中的空格。