啓用mod_rewrite的,並通過httpd.conf
的.htaccess,然後把這個代碼在你.htaccess
下DOCUMENT_ROOT
目錄:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-d # `!-d` means if directory doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f # if file doesn't ...
RewriteCond %{REQUEST_FILENAME} !-l # if link doesn't
# L = last rule, stop processing
# QSA = Query String Append
# R = Redirect (default is 302, temporary)
RewriteRule^/err404.html?url=%{REQUEST_URI}&refr=%{HTTP_REFERER} [L,QSA,R]
然後改變你的err404.html這樣的:
<html><body>
<script type="text/javascript">
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
console.log('Query variable %s not found', variable);
}
document.write('sorry! ' + getQueryVariable('url') +
' not found, requesting URL: ' + getQueryVariable('refr'));
</script>
<body></html>
這將導致下面的文字顯示爲不存在的URI像http://domain.com/not-here
在頁面上http://domain.com/sample.html
sorry! /not-here not found, requesting URL: http://domain.com/sample.html
它適用於我,即使沒有'?url =%{HTTP_REFERER}'(因爲我沒有重定向; url保持不變,但404文檔被加載)。 '/ err404.html'與你的根目錄相關,而不是'.htaccess'文件! – Christopher 2013-03-12 14:58:52