Http 如何在锚点中指定协议并仍然使用相对路径?

Http 如何在锚点中指定协议并仍然使用相对路径?,http,https,uri,anchor,href,Http,Https,Uri,Anchor,Href,在个人网站上,总是首选在锚中使用相对路径,以便您可以轻松地将目录结构移动到新网站,但我希望使我的网站的一部分受密码保护(使用.htaccess),因此使用https。在锚的href中是否有一种方法可以指定不同的协议,而无需对www.domain.com进行硬编码 例如: <a href="/student/example">Link</a> 我想把它变成这样: <a href="https: /student/example">Link</a>

在个人网站上,总是首选在锚中使用相对路径,以便您可以轻松地将目录结构移动到新网站,但我希望使我的网站的一部分受密码保护(使用.htaccess),因此使用https。在锚的href中是否有一种方法可以指定不同的协议,而无需对
www.domain.com
进行硬编码

例如:

<a href="/student/example">Link</a>

我想把它变成这样:

<a href="https: /student/example">Link</a>
<script>
function handleLink(a){
    var path = a.getAttribute('href'),
        host = location.hostname;
    location.href = 'https://'+host+path;
    return false;
}
</script>
<a href="/student/example" onclick="return handleLink(this)">Link</a>


上述方法显然不起作用。有什么办法吗?我试图避免使用任何脚本,但我更喜欢使用JavaScript而不是PHP或ASP。

如果不依赖JavaScript动态更改href,就无法做到这一点。将相对URI转换为绝对URI的方式如中所述:

其中,
R
是相对URL,
T
是目标

上面基本上说,如果在相对URI中指定了方案,那么目标URI将是整个相对URI,因此指定方案的唯一方法是指定整个url

如果您想使用基于Javascript的方法,可以使用如下内容动态设置href:
a.href='https://'+window.location.host+a.getAttribute('href')
。其中
a
是您的锚点元素

下面是Javascript版本的演示:


但是,主要由于您遇到的原因,最好将主机名存储在配置文件中,或者从前端控制器的HTTP请求
Host
头中检测主机名。这样,您就可以在生成页面时将其模板化到HTML中。这使您不必在URL生成后使用客户端脚本来修复URL,这可能是可取的,因为并非每个人都启用了Javascript

您还应该使用.htaccess将https添加到站点的特定URL中。确保.htaccess文件包含以下行:

RewriteEngine on
然后尝试在其后面添加此行:

RewriteRule "^/student(/.*)" "https://%{HTTP_HOST}$1" [R=301,L]

在JavaScript中,您可以执行以下操作:

<a href="https: /student/example">Link</a>
<script>
function handleLink(a){
    var path = a.getAttribute('href'),
        host = location.hostname;
    location.href = 'https://'+host+path;
    return false;
}
</script>
<a href="/student/example" onclick="return handleLink(this)">Link</a>

功能handleLink(a){
var path=a.getAttribute('href'),
host=location.hostname;
location.href='https://'+主机+路径;
返回false;
}
但我不会。 如果您希望让用户使用SSL,我首先会将页面重定向到SSL页面

我认为没有“简单”的解决方案。。。 对于这样的目的来说,javascript似乎不是一个好主意

你可以试试:

<base href="https://yourdomain.com/">

放置在文档标题中

你的想法是:

<a href="/test-me1/">regular link 1</a>
<a href="/test-me2/">regular link 2</a>
<a href="/https/test-me3/">secure link</a>

在底部,但在关闭主体之前标记如下:

<script type="text/javascript">
(function(){

    var h = 'yourdomain.com';
    /* could be replaced with something like window.location.host */

    var a =  document.links;

    for (var c = 0; c < a.length; c++) {

        var i = a[c].href.indexOf('/https/');

        if(-1 !== i)
        {
            a[c].href = 'https://' + h + '/' + a[c].href.substring( i + 7 );
            /* where 7 is a length of '/https/' */
        }
    }
})();
</script>

(功能(){
var h='yourdomain.com';
/*可以替换为类似window.location.host的内容*/
var a=document.links;
对于(var c=0;c
甚至简单地说:

<script type="text/javascript">
(function(){

    var a = document.links;

    for (var c = 0; c < a.length; c++) {

        if(-1 !== a[c].href.indexOf('/https/') )
        {
            a[c].href = a[c].href.replace('/https/','').replace('http:','https:');
        }
    }
})();
</script>

(功能(){
var a=document.links;
对于(var c=0;c
这将添加一个额外的HTTP请求,并且在能够使用HTTP和HTTPS提供特定资源的情况下无法工作(它阻止通过HTTP访问资源)。如果资源只能通过HTTPS访问,那么就必须使用类似上面的方法阻止通过HTTP的访问。(基本上可以关闭或不支持等)@Paulpro是正确的,最好将https url设置为服务器端。另一个想法是在基本标记中使用协议相对url。