Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Problems caused by a 404 error page

Featured Replies

I noticed on one of the SEO reports for my website that I did not have a 404 error page. I went about creating the 404 error page and found some code that emails me when someone lands on it to highlight any problems of visitors not finding a page they are after.

 

I upload the new page and it was not too long before I started to receive emails, in fact I received over 100 on day 1 and over 100 on day 2. The vast majority of these emails were from various IPs trying to access wp-login.php I found that this is a login page for WordPress. My website is not a WordPress website.

 

So what became a good idea of getting an email every time someone lands on the 404 error page, became a pain very quickly. What I have done is to create a 301 redirect to index.html which is solved the amount of emails I now get. What I am now going to get is instead of an email, I am going to get a hit on my home page and with show up on Google Analytics.

 

Should I just disable the 404 error page from sending me an email and remove the 301 redirects or is there something else I need to be doing?

Why complicate things. Just create a 404 page using the page template you used for all your other page and add a message that apologises for the missing page.

 

Then just delete the tracking code on that page so it doesn't show up in GA.

Wow, brave move on the email for every 404 but kudos for wanting to investigate making a better user experience. First, can I just urge you to take off the 301 all to the home page, it defeats the purpose of 404 and also leaves you wide open to potential problems. 301 tells the search engines 'hi, I know the page you wanted but I have permanently moved the content to this new page', and in a sinister context if I linked to a random url on your site such as 'yourdomain.com/cheap_meds' the answer to the search engine would be 301, the cheap meds content has moved to the home page instead of 404 wtf are you asking for.

 

Google Search Console will let you know about 404s where the page was indexed but has disappeared, but tracking every 404 would be time consuming, urls on a domain root are pretty much infinite in combinations and parameters. I would suggest not using tracking code on the 404 page, instead make a minimal design page.

 

Take a look at what I use, this is in the head of the 404 page assuming it's kept at root (/404.php) and htaccess uses;

<?php 
$site_url = "http://www.yourdomain.com";
if($_SERVER['HTTP_REFERER']!="") { // They were referred.
   if(substr($_SERVER['HTTP_REFERER'], 0, strlen($site_url))==$site_url) {
        // They followed a link on your site to this location, possibly use your email idea here.
        $msg = "<p>Sorry message for the broken link as it's your fault.</p>";
    } else {
        // They were referred here from somewhere else, you might want to know this and ask them to update their link.
        $referer = parse_url($_SERVER['HTTP_REFERER']);$referer = $referer['host'];
        $msg = "<p>You were incorrectly referred to this page by <b>" . $referer . "</b>.</p>";
    }
} elseif(substr($_SERVER['REQUEST_URI'], 0, strlen("/404.php"))=="/404.php"){
    // They wanted to see your 404 page  - remember the php version shouldn't exist directly
    $msg = "<p>It appears you wanted to see our super duper 404 error page? Well here it is in all its glory; hope you like it!</p>";
    header('HTTP/1.0 404 Not Found');
} elseif(substr($_SERVER['REQUEST_URI'], 0, strlen("/wp-login.php"))=="/wp-login.php" || substr($_SERVER['REQUEST_URI'], 0, strlen("/wp-admin/"))=="/wp-admin/"){
    // They think you run WordPress
    $msg = "<p>It appears you think this is built with WordPress - Hahahahahahaha!</p>";
} else {
    $msg = "<p>Sorry but the file you have requested cannot be accessed, this could possibly be a mis-typed URL or out-of-date bookmark.</p>";
}
echo $msg; ?>

I'm racking my brains as I read a great blog article on ways to improve error pages, if I can remember where I'll post it.

 

Edit: Forgot to say in all cases of messing around with headers make sure you test them, I use http://www.webconfs.com/http-header-check.php

Edited by BrowserBugs

  • Author

Thanks both for posting a reply.



I think in my quest to rectify any current/future broken links from genuine visitors to my website, I have opened up a hornets nest. One thing I have learnt from this experience is of how many times the site gets a hit from potential hackers seeking for an unsecure WordPress site by having some batch process to look for wp-login.php



BrowserBugs Thanks for the code, this looks like something more useful than what I currently have. So what I need to do is include that code into <head></head>



How do I call up the individual messages on the page? Would it be:



<body>
<p>404 Error Page</p>
<?php
echo $msg;
?>
<p>text</p>
</body>

What I would like to do is to still monitor using email notifications, but I will have to wait and see how many emails this would produce and take it from there to either carry on getting email notifications or not.



if($_SERVER['HTTP_REFERER']!="") { // They were referred.
if(substr($_SERVER['HTTP_REFERER'], 0, strlen($site_url))==$site_url) {
// They followed a link on your site to this location, possibly use your email idea here.
$msg = "<p>Sorry message for the broken link as it's your fault.</p>";

The code that I have to email is as follows:



<body>
<p>404 Error Page</p>
<?php
$ip = getenv ("REMOTE_ADDR");
$requri = getenv ("REQUEST_URI");
$servname = getenv ("SERVER_NAME");
$combine = $ip . " tried to load " . $servname . $requri ;
$httpref = getenv ("HTTP_REFERER");
$httpagent = getenv ("HTTP_USER_AGENT");
$today = gmdate("l, j F Y, g:i a ");
$note = "Page Not Found" ;
$message = " ";
$message2 = "$today \n
$combine \n
User Agent = $httpagent \n
$note \n
$httpref ";
$to = "jbloggs@somewhere.co.uk";
$subject = "404 Error Page";
$from = "From: jbloggs@somewhere.co.uk\r\n";
mail($to, $subject, $message2, $from);
echo $message;
?>
</body>

If someone has found a link directing them to my website and it is broken, then I really would like to know as it could be that I have renamed a page and not done a 301 redirect, or it could be that someone has typed the link in wrong on their website.


Edited by GrahamUK33

 

BrowserBugs Thanks for the code, this looks like something more useful than what I currently have. So what I need to do is include that code into <head></head>

 

 

No it needs to go above the head. The thing with headers is once they are sent they can't be resent. When you request a php page anything above the opening <html> is processed before any headers are returned, like the server pauses before returning the header. Normally you request a file, the server looks for the file and if it exists displays it, this by default has a 200 ok header, and if it does not find the file it returns a 404 header, and if you have a custom 404 page it will use that.

 

What you can do with PHP is change headers before the output, and then choose what you want to do about it. This is very powerful if you want clean urls without relying upon the band aid canonical link, but that's another topic.

 

So I've made a basic page for this, you can customise your heart out with styles etc but the principal is the same. Also wrapped your mailer into a function, then you can just call it in any case you want an email, saves it firing all the time.

<?php
// New function if your mailer is working fine, I've not tested the mailer.
function tellOwner($var) {
$ip = getenv ("REMOTE_ADDR");
$requri = getenv ("REQUEST_URI");
$servname = getenv ("SERVER_NAME");
$combine = $ip . " tried to load " . $servname . $requri ;
$httpref = getenv ("HTTP_REFERER");
$httpagent = getenv ("HTTP_USER_AGENT");
$today = gmdate("l, j F Y, g:i a ");
$note = "Page Not Found" ;
$message = " ";
$message2 = "$today \n
$combine \n
User Agent = $httpagent \n
$note \n
$httpref ";
$to = "jbloggs@somewhere.co.uk";
$subject = "404 Error Page";
$from = "From: jbloggs@somewhere.co.uk\r\n";
mail($to, $subject, $message2, $from);
}
$site_url = "http://www.somewhere.co.uk";
if($_SERVER['HTTP_REFERER']!="") { // Gets the referrer
  if(substr($_SERVER['HTTP_REFERER'], 0, strlen($site_url))==$site_url) { 
    // If the referrer start is the same as $site_url then they clicked a link from your website.
    $msg = "<p>Please accept our apologies for this, it appears this broken. Head back to our <a href=\"/\">home page</a>.</p>";
    tellOwner(); // New function to send a mail
  } else {
    // They clicked a link from a 3rd party website.
    $referer = parse_url($_SERVER['HTTP_REFERER']); $referer = $referer['host'];
    $msg = "<p>You were incorrectly referred to this page by <b>" . $referer . "</b>, not too sure why. Head back to our <a href=\"/\">home page</a>.</p>";
    tellOwner(); // New function to send a mail
  }
} elseif(substr($_SERVER['REQUEST_URI'], 0, strlen("/404.php"))=="/404.php"){
    $msg = "<p>It appears you wanted to see our super duper 404 error page? Well here it is in all its glory; hope you like it!</p>";  
    // Because they went to http://www.somewhere.co.uk/404.php it does exist. The server wants to return a header of 200 ok, but we don't want that else we end up with /404.php say in the index, so instead we change to 404 headers so the search engine or crawler thinks it's not here.
    header('HTTP/1.0 404 Not Found');
} else {
	$msg = "<p>Sorry but the page you have requested cannot be accessed, this could possibly be a mis-typed URL or out-of-date bookmark. Head back to our <a href=\"/\">home page</a>.</p>";
}
 ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>404 | Page Not Found</title>
<meta name="robots" content="noindex">
</head>

<body>
<h1>Page not found</h1>
<?php echo $msg; ?>
</body>
</html>
  • Author

Thanks very much for your continued help, it is looks like something that I am after.



What I am getting though is an error:


Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/somewhere.co.uk/404.php:1) in /home/public_html/somewhere.co.uk/404.php on line 41




If I rem out header('HTTP/1.0 404 Not Found'); then the above error does not appear.



Any ideas what it could be?


Edited by GrahamUK33

It means you have got you <head> in the wrong place. It's saying you are already processing header info and then trying to change what's already been processed.

 

Why do you need the email? Your analytics will tell you who landed on your 404 page.

 

Thanks very much for your continued help, it is looks like something that I am after.

What I am getting though is an error:

Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/somewhere.co.uk/404.php:1) in /home/public_html/somewhere.co.uk/404.php on line 41

If I rem out header('HTTP/1.0 404 Not Found'); then the above error does not appear.

Any ideas what it could be?

 

 

Sorry probably my bad, the notes and line break at the top can trigger this, change at the top;

<?php function tellOwner($var) { // << Notice no gap 
$ip = getenv ("REMOTE_ADDR");
....

... this would have been triggered as you were looking at /404.php.

 

Edit: Ah hang on also check your mail loop doesn't echo $message, your original script had it in.

Edited by BrowserBugs

  • Author

When viewing the 404.php page it still throws up and error:

Warning: Cannot modify header information - headers already sent by (output started at /home/ /public_html/somewhere.co.uk/404.php:1) in /home/ /public_html/somewhere.co.uk/404.php on line 38

 

It does not show up if I rem out:

header('HTTP/1.0 404 Not Found');

 

This is after changing the top line to:

<?php function tellOwner($var) { 
$ip = getenv ("REMOTE_ADDR");
....

The email function works.

  • Author

Why do you need the email? Your analytics will tell you who landed on your 404 page.

 

I know Google Analytics would show the traffic going to the page if the code was included on the page, but I don’t want to log a click on that page as it would give me a 100% bounce rate for those clicks being that 99% would be people trying to gain access to wp-login.php

 

I want the email to highlight any issues of people having the wrong link, or pages that I have renamed and forgot to do a 301 redirect. If I get too many emails from it then I can always disable that feature.

When viewing the 404.php page it still throws up and error:

Warning: Cannot modify header information - headers already sent by (output started at /home/ /public_html/somewhere.co.uk/404.php:1) in /home/ /public_html/somewhere.co.uk/404.php on line 38

 

It does not show up if I rem out:

header('HTTP/1.0 404 Not Found');

 

This is after changing the top line to:

<?php function tellOwner($var) { 
$ip = getenv ("REMOTE_ADDR");
....

The email function works.

 

Take a look at https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php as they really cover in depth the typical causes, they also cover using http://php.net/headers_sent to check. Removing the header('HTTP/1.0 404 Not Found'); would stop the error, but i'm pretty sure if you check the 404.php direct without it using http://www.webconfs.com/http-header-check.php you'd be getting a 200 ok which defies the point.

Edited by BrowserBugs

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.