html - 没有 http 协议的链接

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

html - links without http protocol

htmlprotocolshyperlink

提问by Web_Designer

Is there a reason we include the http/ httpsprotocol on the hrefattribute of links?

我们是否有理由在链接的属性中包含http/https协议href

Would it be fine to just leave it off:

把它关掉可以吗:

<a href="example.com">my site</a>

回答by Jukka K. Korpela

The inclusion of the “http:” or “https:” part is partly just a matter of tradition, partly a matter of actually specifying the protocol. If it is defaulted, the protocol of the current page is used; e.g., //www.example.com becomes http://www.example.comor https://www.example.comdepending on the URL of the referring page. If a web page is saved on a local disk and then opened from there, it has no protocol (just the file: pseudo-protocol), so URLs like //www.example.com won't work; so here's one reason for including the “http:” or “https:” part.

包含“http:”或“https:”部分只是传统问题,部分是实际指定协议的问题。如果默认,则使用当前页面的协议;例如,//www.example.com根据引用页面的 URL变为http://www.example.comhttps://www.example.com。如果一个网页保存在本地磁盘上然后从那里打开,它没有协议(只有文件:伪协议),所以像 //www.example.com 这样的 URL 将不起作用;所以这是包含“http:”或“https:”部分的原因之一。

Omitting also the “//” part is a completely different issue altogether, turning the URL to a relative URL that will be interpreted as relative to the current base URL.

省略“//”部分是一个完全不同的问题,将 URL 转换为相对 URL,该 URL 将被解释为相对于当前基本 URL。

The reason why www.example.com works when typed or pasted on a browser's address line is that relative URLs would not make sense there (there is no base URL to relate to), so browser vendors decided to imply the “http://” prefix there.

www.example.com 在浏览器地址行中输入或粘贴时起作用的原因是相对 URL 在那里没有意义(没有与之相关的基本 URL),因此浏览器供应商决定暗示“http:// ”前缀。

回答by Ninja

URLs in href are not restricted to only HTTP documents. They support all the protocols supported by browsers- ftp, mailto, file etc.

href 中的 URL 不仅限于 HTTP 文档。它们支持浏览器支持的所有协议——ftp、mailto、file 等。

Also, you can preceed URL name with '#', to link to a html id internally in the page. You can give just the name or directory path, without a protocol, which will be taken as a relative URL.

此外,您可以在 URL 名称前加上“#”,以链接到页面内部的 html id。您可以只提供名称或目录路径,而无需协议,它将被视为相对 URL。

回答by Lucas Tolle

My solution was to trick the browser with a redirect service, such as bit.ly and goo.gl (which will be discontinued soon), in addition to others.

我的解决方案是使用重定向服务欺骗浏览器,例如 bit.ly 和 goo.gl(即将停止使用)以及其他服务。

When the browser realizes that the url of the shortcuts is https, it automatically releases the link image, the link is released and instead displays the http image, without showing the original link.

当浏览器发现快捷方式的url为https时,会自动释放链接图片,释放链接,而是显示http图片,不显示原链接。

The annoying part is that, according to the access, it will display in the panel control of your redirector, thousands of "clicks", which is actually "display".

恼人的部分是,根据访问,它会显示在您的重定向器的面板控件中,成千上万的“点击”,这实际上是“显示”。

With this experience I'm going to look for a Wordpress plugin for redirection and create my own "redirects links". So I will have https // mysite.com /id → redirect to http link.

有了这个经验,我将寻找一个用于重定向的 Wordpress 插件并创建我自己的“重定向链接”。所以我会有 https // mysite.com /id → 重定向到 http 链接。

回答by Kumar Bibek

Without http, it would take the relative URL. If you have to point to an external URL, http needs to be specified, else it would try to take you a local resource.

如果没有 http,它将采用相对 URL。如果您必须指向外部 URL,则需要指定 http,否则它会尝试为您获取本地资源。

So, it depends on where you want to point to.

因此,这取决于您要指向的位置。