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