使用相对链接从https(反之亦然)链接到http的简短方法

jer*_*oen 20 html https http

当我正在开发的网站的安全部分(使用https输入特定页面或文件夹)时,与普通页面的相对链接也会自动转换为https.

有没有办法告诉<a>标签始终使用http协议而不是https协议(或其他方式)?

我知道使用完整链接(http://www.mysite.com/index.htmlhttps://www.mysite.com/index.html)很容易,但我希望它可以使用相关链接(index.html,../index.html等).

Mat*_*hew 29

您可以使用以下语法使所有内部链接独立于协议:

<a href="//some/file/on/your/domain.php">link text</a>

代替

<a href="some/file/on/your/domain.php">link text</a>

//将使链接尊重页面加载的任何协议.

  • 启动带有两个斜杠的URI是"网络路径引用",只要您指定域**,就像Matt描述的**一样.您不能将此语法用于本地文件,因为示例似乎表明(浏览器会尝试解析"some",就好像它是一个域).请参阅[URI以两个斜杠开头......它们的行为如何?](http://stackoverflow.com/questions/4071117/uri-starting-with-two-slashes-how-do-they-behave) (5认同)

小智 10

我们使用Apache mod_rewrite来控制页面是通过http还是https提供的.以下是网站根目录.htaccess文件中的示例代码段:

# Redirect most reqests for https to http
RewriteRule ^https://www.example.com(.*) http://www.example.com$1 [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/$1 [L,R]

# Force some URIs to be https only
RewriteCond   %{SERVER_PORT}  ^80$
RewriteRule ^store(.*) https://www.example.com/store$1 [L,R]

RewriteCond   %{SERVER_PORT}  ^80$
RewriteRule ^FormSanityKey.htm https://www.example.com/login$1 [L,R]
Run Code Online (Sandbox Code Playgroud)

阅读所有相关信息:

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html