2017-01-03 65 views
0

我試圖刪除以下URL中的#:(www.example.com/#section1)。我怎麼能使用htaccess文件來做到這一點。我相信這可以使用正則表達式來完成,但我不知道我會如何做到這一點。使用HTML網頁中的htaccess從URL中刪除'#'

這是我的htaccess文件RewriteRule ^[#]內寫入。

感謝您的幫助!

+0

您是否嘗試過?如果是,什麼?如果不是,爲什麼不呢? – Tomalak

+0

你好。我試圖使用重寫引擎,但我一直不成功。你有任何想法如何解決這個問題。 –

+0

這不是它的工作原理。你發佈你已經嘗試過的東西,並解釋你卡在哪裏,我們會幫助你。這涉及到你發佈的代碼可以顯示你自己解決任務的努力。 – Tomalak

回答

1

哈希(#)不發送到服務器,所以你不能操縱它們的服務器上。

如果你真的需要將其刪除,你將不得不使用JavaScript的每一頁上。

// Wait for the page to load, and call 'removeHash'. 
document.addEventListener("DOMContentLoaded", removeHash); 
document.addEventListener("Load", removeHash); 

function removeHash() { 
    // If there is no hash, don't do anything. 
    if (!location.hash) return; 

    // http://<domain></pathname>?<search><#hash> 

    // Build an URL for the page, sans the domain and hash 
    var url = location.pathname; 
    if (location.search) { 
     // Include the query string, if any 
     url += '?' + location.search; 
    } 

    // Replace the loaded url with the built url, without reloading the page. 
    history.replaceState('', document.title, url); 
} 
+0

謝謝Markus!該代碼已成功從網址中刪除#號。但是,你能解釋一下你寫了什麼,因爲我不明白這一切。我真的很感謝你的幫助:) –