通过启用 mod_rewrite 和 .htaccess httpd.conf
,然后将此代码放在您.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