Html 为什么锚标记的 href 值需要在 URL 前加上 http://?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8997790/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:45:34  来源:igfitidea点击:

Why does an anchor tag's href values need http:// preprended to the URL?

html

提问by chandmk

<a href="www.stackoverflow.com" target="_blank">click here</a>

Clicking the above link on a site's HTML page would try to take the user to

单击站点 HTML 页面上的上述链接会尝试将用户带到

<site>/index.html/www.stackoverflow.com

Where as following works fine

以下工作正常

<a href="http://www.stackoverflow.com" target="_blank">click here</a>

What is the rationale for this behavior?

这种行为的理由是什么?

采纳答案by jmort253

There are several protocols: HTTP, HTTPS, FILE, SSH, SSL, FTP. In addition, as Jeremy mentioned, it's quite possible you may have a file on your server with the exact name of the text you're entering in the HREF.

有几种协议:HTTP、HTTPS、FILE、SSH、SSL、FTP。此外,正如 Jeremy 所提到的,您的服务器上很可能有一个文件,其中包含您在 HREF 中输入的文本的确切名称。

Basically, any text in the href without a protocol is assumed to be a relative path if there is no /or protocol.

基本上,如果没有协议,href 中没有协议的任何文本都被假定为相对路径/

回答by iono

Update 3/2/2016:

2016 年 3 月 2 日更新:

Just use HTTPS for everything, eg: https://www.example.com

只需对所有内容使用 HTTPS,例如: https://www.example.com

Paul Irish recommends against using protocol-relative URLs, i.e. //, and recommends writing all links using https://- the rationale being:

Paul Irish 建议不要使用协议相对 URL,即//,并建议使用https://-编写所有链接- 理由是:

Now that SSL is encouraged for everyone and doesn't have performance concerns, this technique [protocol-relative URLs] is now an anti-pattern. If the asset you need is available on SSL, then alwaysuse the https://asset.

Allowing the snippet to request over HTTP opens the door for attacks like the recent Github Man-on-the-side attack. It's always safe to request HTTPS assets even if your site is on HTTP, however the reverse is not true.

现在每个人都鼓励使用 SSL 并且没有性能问题,这种技术 [protocol-relative URLs] 现在是一种反模式。如果您需要的资产在 SSL 上可用,则始终使用该https://资产。

允许片段通过 HTTP 请求为最近的 Github Man-on-the-side 攻击等攻击打开了大门。即使您的站点在 HTTP 上,请求 HTTPS 资产始终是安全的,但反之则不然。



Original post:

原帖:

As other answers have stated, including protocols in URIs is important because otherwise you leave it up to chance how the URI is interpreted. You could have a MP4 video file literally named "www.something.com" instead of "video.mp4", or a client might try to access your website over FTP because it guessed the protocol wrong.

正如其他答案所述,在 URI 中包含协议很重要,否则您将决定如何解释 URI。您可以将 MP4 视频文件字面命名为“www.something.com”而不是“video.mp4”,或者客户端可能会尝试通过 FTP 访问您的网站,因为它猜错了协议。

As Kolink pointed out in a comment, you can omit the http:entirely and just use //, eg //www.example.com. It stops mixed-content security errors ("this page has insecure elements"). It does this because browers will fetch assets such as images as if those assets were using https://when the user is connected to the current page via HTTPS.

正如 Kolink 在评论中指出的那样,您可以http:完全省略 ,而只使用//,例如//www.example.com。它会阻止混合内容安全错误(“此页面包含不安全元素”)。这样做是因为浏览器将获取诸如图像之类的资产,就像https://当用户通过 HTTPS 连接到当前页面时正在使用这些资产一样。

回答by Kennith Nichol

If you leave off the protocol (http, https, ftp, mailto, etc...) the link will be relative to the current request URI. It is the same as prefixing the href with a ./ meaning this directory.

如果您不使用协议(http、https、ftp、mailto 等),则链接将与当前请求 URI 相关。它与使用 ./ 表示该目录的 href 前缀相同。

browser address bar is http://domain.com/questions/

浏览器地址栏是 http://domain.com/questions/

<a href="www.stackoverflow.com"becomes domain.com/questions/www.stackoverflow.com

<a href="www.stackoverflow.com"变成 domain.com/questions/www.stackoverflow.com

<a href="./www.stackoverflow.com" becomes domain.com/questions/www.stackoverflow.com

<a href="./www.stackoverflow.com"变成 domain.com/questions/www.stackoverflow.com



If you prefix with a / you will get from the domain root <a href="www.stackoverflow.com"becomes domain.com/www.stackoverflow.com

如果你用一个前缀/你会得到域根 <a href="www.stackoverflow.com"domain.com/www.stackoverflow.com



By adding the http:// (or any other protocol) the browsers knows this link is meant to be external to the current page. It is an absolute path because it contains the protocol and full path information.

通过添加 http://(或任何其他协议),浏览器知道此链接是当前页面的外部链接。它是一个绝对路径,因为它包含协议和完整路径信息。

<a href="http://www.stackoverflow.com"becomes http://www.stackoverflow.com

<a href="http://www.stackoverflow.com"变成 http://www.stackoverflow.com

The link can also be protocol-relativein order to easily avoid mixed content security errors.

链接也可以是协议相关的,以便轻松避免混合内容安全错误。



Furthermore, if you specify the <base href="http://domain.com/dir/">in the (* it must be an absolute URL) Then all non-protocol prefixed URLs in href= and src= will be based on that base URL instead of the current page.

此外,如果您<base href="http://domain.com/dir/">在 (* 它必须是绝对 URL) 中指定 ,那么 href= 和 src= 中的所有非协议前缀 URL 将基于该基本 URL 而不是当前页面。

assuming the same address bar of http://domain.com/questions/and the <baseset above.

假设http://domain.com/questions/<base上面设置的地址栏相同。

<img src="file.jpg"becomes http://domain.com/dir/file.jpg<a href="www.stackoverflow.com"becomes http://domain.com/dir/www.stacoverflow.com

<img src="file.jpg"http://domain.com/dir/file.jpg<a href="www.stackoverflow.com"http://domain.com/dir/www.stacoverflow.com

回答by Brill Pappin

Because the URL could have any number of protocols:

因为 URL 可以有任意数量的协议:

The browser doesn't know if you mean ftp://www.stackoverflow.com, http://www.stackoverflow.comor whatever, so you need to specify.

浏览器不知道你的意思是ftp://www.stackoverflow.comhttp://www.stackoverflow.com还是其他什么,所以你需要指定。