Skip to content

Instantly share code, notes, and snippets.

@dennyhalim
Created July 13, 2009 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennyhalim/146055 to your computer and use it in GitHub Desktop.
Save dennyhalim/146055 to your computer and use it in GitHub Desktop.
<?php
//
// code licensed http://creativecommons.org/licenses/by-nc/3.0/
// plus, one rule. a link to http://dennyhalim.com from wherever this code used
//
// first of all, add this line (without //) to .htaccess:
// ErrorDocument 404 /404.php
//
// you dont need to edit anything here
// unless you want to change the looks & feel to match your website you can edit the html at the bottom
//
ob_start();
$browser = $_SERVER['HTTP_USER_AGENT'];
$msie = 'Mozilla/4.0 (compatible; MSIE ';
$lotsbrowsers = 'Mozilla/';
//$cek = strpos($browser,$msie);
//nowadays... too much toolbars/browsers/etc hijacking 404 error pages. let give them all soft-404
$cek = strpos($browser,$lotsbrowsers);
// now that we lie to msie that this is not 404 error page,
// it's safe to remove all the too much useless comments at the html section
if ($cek === false) {
header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");
} else {
header("HTTP/1.1 200 OK");
header("Status: 200 OK");
}
header('X-Powered-By: http://wiki.dennyhalim.com/404-php');
?>
<!--//
404.php is a better, friendly solution to 404 not found page
(c) 2007 Denny Halim ST S.Kom ; Polaris Network Indonesia
http://www.DennyHalim.com - http://www.PolarisNetwork.com
http://wiki.dennyhalim.com/404-php
http://creativecommons.org/licenses/by-nc/3.0/
//-->
<?php
// you can also override $domain = 'www.yourdomainname.com';
// especially if the server http host return wrong answer like 127.0.0.1 or localhost
$domain = str_replace('www.', '', $_SERVER['HTTP_HOST']);
$url = $_SERVER['REQUEST_URI'];
$path = parse_url($url, PHP_URL_PATH);
$path_parts= pathinfo($path);
$path = $path_parts['dirname'];
$path= urldecode(stripslashes($path));
$namafile = $path_parts['filename'];
$pisah = array("_", "/", "-", "+");
$path= str_replace($pisah, " ", $path);
$namafile = str_replace($pisah, " ", $namafile);
$dicari = $path . " " . $namafile;
// choose either ajaxsearch or standard search form
//$mencari = 'http://ajaxsearch.jgate.de/?domain=' . $domain . '&dicari=' . $dicari;
$mencari = 'http://cari.jgate.de/?big=1&status=404&domain=' . $domain . '&dicari=' . $dicari;
?>
<html>
<head>
<!--//
you can put up some css styling here
you might want to leave all these useless comments
if you remove all these comments,
this custom 404 page might not work on Microsoft Internet Explorer browsers
also, might not work when google toolbar is installed.
try google to find out why...
you can edit the html code below to suite your website looks & feel
you must leave the link back to http://wiki.dennyhalim.com/404-php
//-->
<META NAME="ROBOTS" CONTENT="NOINDEX, FOLLOW">
<title>Status: 404 Not Found</title>
</head>
<body>
<h2>Status: 404 Not Found</h2>
<h4>The Page does not exist.</h4>
<p>
<!--// you can also add more links or menu or sitemap etc... //-->
<p>Suggestions:</p>
<ul>
<li>try <a href="http://<?php echo $_SERVER['HTTP_HOST']; ?>"><?php echo $_SERVER['HTTP_HOST']; ?></a> or <a href="http://<?php echo $domain; ?>"><?php echo $domain; ?></a> </li>
<li>check for typos. maybe the link mistyped. try removing any spaces in the url. </li>
<li>find a copy at <a href="http://www.google.com/search?q=cache:<?php echo $_SERVER['HTTP_HOST']; echo $url; ?>">Google</a> or <a href="http://web.archive.org/web/*/<?php echo $_SERVER['HTTP_HOST']; echo $url; ?>">Wayback Machine</a></li>
<li>go <a href="javascript:history.back()">back</a> where you came from </li>
<li>search</li>
</ul>
<hr>
<div id="content">
<p>Javascript is loading Your <a href="<?php echo $mencari; ?>">search on <?php echo $domain; ?> for <?php echo $dicari; ?></a><br/>
if you disabled javascript, it will never loaded.</p>
<p><a href="http://wiki.dennyhalim.com/404-php" title="better, friendly custom 404 error page">get your own 404.php</a></p>
</div>
<!--// let's do some cool ajax search for our visitors //-->
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('search', '1');
function OnLoad() {
var customSearchControl = new google.search.CustomSearchControl('003127643427034444210:vcvoo5yqjhg');
customSearchControl.draw('content');
customSearchControl.execute('<?php echo $dicari; ?> site:<?php echo $domain; ?>');
}
google.setOnLoadCallback(OnLoad);
</script>
<noscript>
<iframe src="<?php echo $mencari; ?>" width="100%" height="2000" frameborder="0" scrolling="no"></iframe>
<p><a href="http://wiki.dennyhalim.com/404-php" title="better, friendly custom 404 error page">get your own 404.php</a></p>
</noscript>
<NOFRAMES>
<a rel="nofollow" href="<?php echo $mencari; ?>">click here to search <?php echo $domain; ?> for <?php echo $dicari; ?></a>
<p><a href="http://wiki.dennyhalim.com/404-php" title="better, friendly custom 404 error page">get your own 404.php</a></p>
</NOFRAMES>
</body>
</html>
<?php ob_end_flush();
#make it works globally on all vhost
#put everything in /home/error
#then put this file in /etc/apache2/conf.d/
# git clone git://gist.github.com/146055.git /home/error
# cp /home/error/error-handler.conf /etc/apache2/conf.d/
# /etc/init.d/apache2 reload
# keep update using:
# cd /home/error
# git pull
Alias /error/ "/home/error/"
ErrorDocument 404 /error/404.php
<Directory "/home/error">
#AllowOverride None
Options IncludesNoExec
#AddOutputFilter Includes html
#AddHandler type-map var
Order allow,deny
Allow from all
LanguagePriority en cs de es fr it nl sv pt-br ro
ForceLanguagePriority Prefer Fallback
</Directory>
#AliasMatch ^/(robots\.txt|favicon\.ico)$ /home/error/$0
<Files favicon.ico>
ErrorDocument 404 /error/favicon.ico
</Files>
<Files robots.txt>
ErrorDocument 404 /error/robots.txt
</Files>
User-agent: *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment