2013-06-11 37 views
7

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

例子:

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

我希望把它变成类似:

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

以上显然是行不通的。有什么办法可以做到吗?我试图避免任何脚本,但会比PHP或ASP更赞赏JavaScript。

+0

欢迎来到StackOverflow。这是一个很好的问题。 – Paulpro

回答

0

如果不依靠Javascript来动态更改href,你不能这样做。相对的URI转化为绝对URI的方式RFC 3986 Section 5.2.2描述:

if defined(R.scheme) then 
    T.scheme = R.scheme; 
    T.authority = R.authority; 
    T.path  = remove_dot_segments(R.path); 
    T.query  = R.query; 
else 
    if defined(R.authority) then 
     T.authority = R.authority; 
     T.path  = remove_dot_segments(R.path); 
     T.query  = R.query; 
    else 
     if (R.path == "") then 
     T.path = Base.path; 
     if defined(R.query) then 
      T.query = R.query; 
     else 
      T.query = Base.query; 
     endif; 
     else 
     if (R.path starts-with "/") then 
      T.path = remove_dot_segments(R.path); 
     else 
      T.path = merge(Base.path, R.path); 
      T.path = remove_dot_segments(T.path); 
     endif; 
     T.query = R.query; 
     endif; 
     T.authority = Base.authority; 
    endif; 
    T.scheme = Base.scheme; 
    endif; 
    T.fragment = R.fragment; 

哪里R是相对URL和T是目标。

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

如果你想要使用基于Javascript的方法,你可以使用类似的动态设置href:a.href = 'https://' + window.location.host + a.getAttribute('href')a是你的主播元素。

这里是JavaScript版演示:http://jsfiddle.net/7DWV5/

然而,在很大程度上为您所遇到的原因,这是一个好主意,无论是存储在配置文件中的主机名,或检测到它前端控制器中的HTTP请求Host标头。这样,您可以在生成页面时将其模板化为HTML。这样可以节省您不必使用客户端脚本在生成后修复您的网址,这可能是可取的,因为不是每个人都启用了Javascript。

0

您应该使用.htaccess将https添加到您网站的特定网址。请确保你的.htaccess文件中包含这一行:

RewriteEngine on 

然后尝试后,加入这一行:

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

http://www.askapache.com/htaccess/ssl-example-usage-in-htaccess.html#redirect-urls-to-ssl

+0

这将添加额外的HTTP请求,并且无法使用HTTP和HTTPS(它阻止通过HTTP访问资源)提供特定资源。如果资源只能通过HTTPS访问,那么就需要像上面那样阻止通过HTTP访问的内容。 – Paulpro

0

在JavaScript中,你可以这样做:

<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> 

我不会。 如果你热衷于保持SSL的用户,我会把页面重定向到一个SSL页面

1

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

你可以尝试:

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

放置在文件头。

OR

你的想法:

<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> 

甚至干脆:

<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> 
+0

再一次,JavaScript不是这样的好主意。 (基本上可以关闭或不支持等)。 @Paulpro是正确的,更好地使您的https urls服务器端。 – Chris

+1

另一个想法是在基本标记中使用协议相对url。 – Chris

相关问题