Archive for the ‘Server Side’ Category

Get Notified of 404 Errors

Saturday, January 13th, 2007

One addition I have found useful is to add email notification when my custom 404 page is invoked. Since I use a PHP file for the custom 404 adding email notification is pretty easy. I renamed the original custom404.htm to custom404.php then added the email code.

The email sent will spell out the invalid page that caused the 404 error. BTW, 404 is the error generated by HTTP when an invalid or nonexistent web page is trying to be accessed. The commented out echo code was used for diagnostics.

Note that nothing is shown on the custom 404 page. The email occurs behind the scenes.

Here is the code:


<?php
// send error email
$em = "someone";
$em .= "@";
$em .= "mydomainname";
$em .= ".com";

$subject="Custom404";
$message="A custom 404 page was accessed from mysecurepc site";
$message .= " The page accessed was: " . $_SERVER['REDIRECT_SCRIPT_URI'];

if(mail("$em","$subject","$message","From: $em")){
//echo "e-mail sent !!!";
}else{
//echo "error !!!";
}
?>

A few things I have noticed since I set this up on a couple of web sites for testing.

1. For some reason, I get a custom 404 email when a bookmark like

...domain.com/folder/webpage.html#topofpage

is accessed. I have not been able figure out why, yet.

2. If you do not have a favicon.ico then the custom 404 page is invoked and you will get email. This occurs when IE accesses the home page.

3. Deleted web pages may show up in an 404 email since search engines retain these links for a while. The searchbots invoke these.

4. Deleted or name-changed web pages may show up because someone is using an old link.

Overall it has given me valuable feedback despite some unwanted emails.

Doug

Check your HTTP Header

Saturday, November 11th, 2006

A tool to check your HTTP header is very useful, indeed. For example, once a 301 redirect is done, check it to make sure it works. If you check the header for http://smartlabsoftware.com you will see that it returns in the receiving header, a status of 301 and the new location will be http://www.smartlabsoftware.com

Checking http://www.smartlabsoftware.com/invalid.htm will produce a 404 return code (file not found).

Other bits of information, such as what server (e.g. Apache)  and its version (e.g. 1.3.33) are useful to know.
Doug

Uploading the .htaccess File

Saturday, November 11th, 2006

Sometimes uploading the .htaccess file to the Apache server does not work; especially in the Windows environment. Try this:

  1. Rename the .htaccess file to htaccess.txt
  2. Upload it to the server
  3. Rename htaccess.txt to .htaccess

Step 3 can be tricky. If your FTP program or web designer program (such as FrontPage) won’t allow you to rename the htaccess.txt file try using the FTP program supplied by your host. Once renamed it may take a few minutes for the .htaccess file to take effect.

Another way around this dilemma is to use a 3rd party ftp program. I like FileZilla which is free and easy to use. The .htaccess and .htpasswd files can directly be uploaded without changing their names.

Doug

www vs no www

Saturday, November 11th, 2006

Note this only works on an Apache server with mod_rewrite turned on.
Google recommends using www or not www but not both. In other words, use www.mydomain.com or mydomain.com but not both. Using a 301 permanent redirect can alleviate the problem.
To redirect mydomain.com to www.mydomain.com do the following:

In an existing .htaccess file or a new .htaccess file add the following code:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

Replace mydomain with the domain you want to redirect. r=301 means redirect the client and send a status code of 301.

Doug