2013-06-11 36 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

相關問題