2012-09-14 78 views
4

出於某種原因,當我header("Location")重定向新的頁面保持哈希值。重定向是保持哈希

所以,如果你對example.com/index.html#signup

我重定向與

header("Location: /account.html"); 
exit; 

但後來它顯示example.com/account.html#signup

這究竟是爲什麼以及如何我停止嗎?即example.com/account.html


注:

我使用一個.htaccess重定向到file.htmlfile.php

RewriteRule ^([a-zA-Z0-9-_.]+)\.html$ $1.php [L] 

回答

4

簡單的回答「我怎麼阻止它」是指定一個空的哈希在Location標頭中:

header('Location: /account.html#'); 

然而,這種行爲並不能保證全面。它似乎可以在我的快速測試中用於WebKit和IE9。儘管如此,你已經無意中發現了black hole in the HTTP specification

+1

這並不在Firefox上運行 – machineaddict

1

我四處尋找答案,關於Firefox的作​​品。一會兒衝浪在這裏和那裏,而我的貓喵保持喵後,使我最終的解決方案:

die('<script> window.location='your-url-without-hash';</script>');

有時你想保留的哈希,有時你不這樣做,因爲交叉瀏覽器很重要,所以最好控制客戶端瀏覽器重新加載並殺死哈希。