2017-01-03 67 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!该代码已成功从网址中删除#号。但是,你能解释一下你写了什么,因为我不明白这一切。我真的很感谢你的帮助:) –