Html 使用相对链接从 https(反之亦然)链接到 http 的快捷方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/584251/
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
Short way to link to http from https (and vice versa) using relative links
提问by jeroen
When I′m in a secure part of a web-site I′m developing (entering a specific page or folder using https), the relative links to the normalpages are automatically converted to https as well.
当我在我正在开发的网站的安全部分(使用 https 进入特定页面或文件夹)时,正常页面的相关链接也会自动转换为 https。
Is there a way to tell an <a> tag to always use the http protocol instead of the https protocol (or the other way around)?
有没有办法告诉 <a> 标签总是使用 http 协议而不是 https 协议(或其他方式)?
I know it′s easy using the complete link (http://www.mysite.com/index.htmlor https://www.mysite.com/index.html), but I would like it to work with relative links (index.html, ../index.html, etc.).
我知道使用完整链接(http://www.mysite.com/index.html或https://www.mysite.com/index.html)很容易,但我希望它使用相对链接(index.html、../index.html 等)。
采纳答案by Jans Carton
We use Apache mod_rewrite to control whether a page is served via http or https. Here's an example snippet from a site's root directory .htaccess file:
我们使用 Apache mod_rewrite 来控制页面是通过 http 还是 https 提供的。以下是来自站点根目录 .htaccess 文件的示例片段:
# Redirect most reqests for https to http
RewriteRule ^https://www.example.com(.*) http://www.example.com [R=301,NC]
# Allow some URIs to be https if requested
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^/images/(.*)$
RewriteCond %{REQUEST_URI} !^/scripts/(.*)$
RewriteCond %{REQUEST_URI} !^/styles/(.*)$
RewriteCond %{REQUEST_URI} !^/store(.*)$
RewriteCond %{REQUEST_URI} !^/login.htm$
RewriteRule ^(.*) http://www.example.com/ [L,R]
# Force some URIs to be https only
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^store(.*) https://www.example.com/store [L,R]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^FormSanityKey.htm https://www.example.com/login [L,R]
Read all about it at:
在以下位置阅读所有相关信息:
回答by Matthew
You can make all your internal links be protocol independent by using the following syntax:
您可以使用以下语法使所有内部链接独立于协议:
<a href="//some/file/on/your/domain.php">link text</a>
<a href="//some/file/on/your/domain.php">link text</a>
instead of
代替
<a href="some/file/on/your/domain.php">link text</a>
<a href="some/file/on/your/domain.php">link text</a>
the //
will make the link respect whatever protocol the page was loaded over.
这//
将使链接尊重页面加载的任何协议。
回答by David Z
Not sure if this is exactly what you were looking for, but if you're using Apache, there's a trick you can use to do this: http://httpd.apache.org/docs/2.2/ssl/ssl_faq.html#relative
不确定这是否正是您要查找的内容,但如果您使用的是 Apache,则可以使用一个技巧来执行此操作:http: //httpd.apache.org/docs/2.2/ssl/ssl_faq.html#相对的
Basically you put the following in your Apache configuration file:
基本上,您将以下内容放入 Apache 配置文件中:
RewriteEngine on
RewriteRule ^/(.*):SSL$ https://%{SERVER_NAME}/ [R,L]
RewriteRule ^/(.*):NOSSL$ http://%{SERVER_NAME}/ [R,L]
then append :SSL
to any link (in the webpage) for which you want to force HTTPS, and :NOSSL
to any link for which you want to force regular HTTP.
然后附加:SSL
到您想要强制使用 HTTPS 的任何链接(在网页中),以及:NOSSL
您想要强制使用常规 HTTP 的任何链接。
回答by Damien McGivern
There is no way to do this with relative hyperlinks as your are using a different protocol, it would be no different than linking to a ftp location.
由于您使用的是不同的协议,因此无法使用相对超链接来做到这一点,这与链接到 ftp 位置没有什么不同。
回答by Nick Presta
It sounds like you want to use the BASE element.
听起来您想使用BASE 元素。
There is no way to 'tell' an Anchor to use a specific protocol or not, at least without using the BASE element in the HEAD of that page.
无法“告诉”Anchor 是否使用特定协议,至少不使用该页面 HEAD 中的 BASE 元素。
Is there a reason why you want to use relative links over absolute links? This articletalks about the pitfall of using relative links with HTTPS - the potential to have your site double indexed and such.
有什么理由要使用相对链接而不是绝对链接吗?本文讨论了在 HTTPS 中使用相对链接的陷阱 - 可能使您的网站被双重索引等。
回答by Daniel De León
I use this code to create a link of the current index page to direct to the https
server, because we provide different content according the protocol and we do not know where is hosting.
我用这段代码创建了当前索引页面的链接指向https
服务器,因为我们根据协议提供不同的内容,我们不知道托管在哪里。
<a>Secure server
<script>
let url = new URL(window.location);
url.protocol = 'https:';
url.port = '443';
document.currentScript.parentElement.href = url.toString();
</script>
</a>