What is Error 404 Page? How to configure Error 404 page? part 2

1 comments | 1,036 views

Error 404 Apache : How to configure Error 404 page in Apache Server

It is extremely easy to configure the Apache server to serve your customized error 404 page.As mentioned in my previous post, create an error404page.html page for your server to serve whenever the visitors looking for a page which does not exists.

In your server you can find httpd.conf file.

Error 404 Apache Server

Open the conf file. At the bottom of the file you can find the section as mentioned in the image. This is where you have to edit.

These directives shown in the image would have been commented by default. What you have to do is, just un-comment the directive for ErrorDocument and then change the path to point to your customized error 404 page.

ErrorDocument 404 / error404page.html

If you are not able to find and edit the conf file, create an .htaccess file with your text editor. Add the following line in it and save.

ErrorDocument 404 / error404page.html

Now upload .htaccess file in your root folder where your index file is present

After uploading search for a page which is not present in you site and check whether the Apache server serves the customized error 404 page.

IIS Error Page – How to Configure Custom Error 404 Page in IIS Server

My previous posts on Error 404 page explained about What is Error 404 page and its benefits, 10 Tips to create Custom 404 error page, How to find and fix broken links using Google Webmaster tools and How to configure Error 404 page in Apache Server.

Now let us find how to configure Error 404 page in Microsoft IIS Server.

To configure IIS server, you should either be an Administrator of the web server or Have access to an IIS admin interface.

Before configuration, check your error404page whether it displays the contents of the page properly.

Add custom Error 404 Page to IIS:

1. To open the Internet Information Services (IIS) snap-in, use the Microsoft Management Console.

2. If you are using Windows 2000, select the icon ‘My Computer’, right click and choose ‘Manage’.

3. Under ‘Services and Applications’, you can find the Internet Information Services.

IIS Computer Management

4. Find your website, Select, right click and choose ‘Properties’.

IIS Website Properties

5. In the Properties window, Select the ‘Custom Errors’ tab.

IIS Custom Error Tab

6. Under the ‘Custom Errors’ tab, scroll down through the error codes to find ‘404′ Page Not Found.

IIS Custom 404 HTTP Error

7. Choose ‘404′ and select ‘Edit Properties’. Select ‘Message Type: URL’ and enter the path, from the root document to your ‘error404page’.

IIS Error Mapping Properties

8. Now, Click ‘OK’ to activate your changes.

Check your changes by requesting a page that does not exists in your site. We should able to see the output of the page.

How to Configure Error 404 Page in ASP for IIS Server?

Let us assume you have created an .asp file called error404page.asp, which is customized to handle HTTP 404 errors. When a visitor to your site tries to access a page that does not exist on your server, say a page called missedpage.asp, then the ASP file will be invoked as if the user had typed:
‘http://www.example.com/error404page.asp?404;http://www.example.com/missedpage.asp’ in the Web browser.

From the information with the above query string, “404;http://www.example.com/missedpage.asp”, we can show the user the page they have requested, and send instant information to the administrator. Let us see how this can be done.

Save the file error404page.asp in the root folder of your web site which you have created to handle the HTTP 404 errors. As mentioned in my previous post ‘How to configure Error 404 page in IIS Server’ configure the custom 404 message of your web site to point to the URL, error404page.asp.

To track the URL that triggered the HTTP 404 Error, we have to write a code in the asp page error404page.asp with the help of Request.query string. Then we have to clean the captured query string to get the requested URL and send a mail to the site administrator using CDONTS. We will then display the page to the user after completing all the server side processing.

In the ASP page, error404page.asp, copy the following code to display to the user when a HTTP 404 error occurs:

< %@Language="VBScript" %>
< %
'error404page.asp - ASP file for handling Customer Error
'Messages mapped to a URL
Dim capturedQryStrg
Dim requestedURL
'As I explained above, the entire query string starts with
'the error number, followed by the requested URL
capturedQryStrg = Request.ServerVariables("Query_String")
'Once we have captured the entire query string, in this
'case "404;http://www.example.com/missedpage.asp", let us
'clean it by removing the "404;", leaving us with just the
'requested URL
requestedURL = Replace(capturedQryStrg, "404;", "")
'After we are done with cleaning up, we will send a mail
'to the site administrator using the CDONTS object.
'We will mark the email as urgent.
Dim errMail
Set  errMail = Server.CreateObject("CDONTS.NewMail")
errMail.From = "webuser@example.com"
errMail.To = "administrator@example.com"
errMail.Subject = "URGENT - 404 Error"
errMail.Body = "User requested - " & requestedURL
errMail.Importance = 2
errMail.Send
Set errMail = Nothing
'We are done with the server-side processing.
'We will now display the content to the user.
%>
<html>
<head>
<title>Sorry, but < % = requestedURL %> was not found on example.com</title>
</head>
<body>
The page you requested, < %= requestedURL %>was not found on
example.com<br />
It's possible you typed the address incorrectly, or that the page no
longer exists. <br />
An email alert has been sent to the administrator regarding this error.<br />
As an option, you may visit any of the pages below for information about our services or products:<br />
Home<br />
Products<br />
Contact Us
</body>
</html>

ASP.NET 404 ERROR PAGE – How to Configure Error 404 Page in ASPX (.NET) for IIS Server?

My previous post explained about How to create Error 404 page in asp. This method will work out for all extensions except ASPX files. In ASPX (.NET) these files pass through special parser which instead outputs a .NET error. If we want the .NET application redirect to custom Error 404 page, We have to edit our web.config file in the root of the website.

Open your Web.config file, present in the root of the site. If you do not have a web.config file create one. Copy and paste the following in the file.

<configuration>
<system .web>
<customerrors mode="On">
<error statusCode="404" redirect="/errors/error404page.aspx" />
</customerrors>
</system>
</configuration>

With the use of the above code we are instructing the .NET application to turn on custom errors and for 404 response code we redirect to a specific virtual page.
In the ASPX page, error404page.aspx, copy the following code to display to the user when a HTTP 404 error occurs:

< %@ Page Language="VB" runat="server" explicit="true" strict="true" %>
< %@ Register TagPrefix="UserControl" TagName="Header" Src="head.ascx" %>
< %@ Register TagPrefix="UserControl" TagName="Footer" Src="tail.ascx" %>
< %@ Import Namespace="System.Web.Mail" %>

<script language="vb" runat="server">
Sub Page_Load()
'-- Get Query from the URL
Dim strQuery As String = Request.ServerVariables("QUERY_STRING")
'-- avoid running our code if someone browses
' the 404.aspx file directly
If strQuery <> "" Then
'-- Page with the broken link
Dim strReferer As String = Request.ServerVariables("HTTP_REFERER")
'-- remove the default appended string to the url
strQuery = strQuery.Replace("404;http://", "")
'-- Find the position of the root "/" in the string
Dim intSlashPosition As Integer = strQuery.IndexOf("/")
'-- Remove all text before the "/"
strQuery = strQuery.Remove(0, intSlashPosition)
'-- set the label control's text
lblQueryString.Text = strQuery
'-- Send Email to the Webmaster
Dim objMail As New MailMessage()
Dim strEmailBody As String
strEmailBody = "Seo-Insights - 404 Page Not Found : " & DateTime.Now & "<br />" & "------------------------------------------------------" & "<br />" & "<br />"
If strReferer = "" Then strEmailBody = strEmailBody & "Someone typed in this broken link directly:" & "<br />" Else strEmailBody = strEmailBody & "The page <a href=""" & strReferer & """>" & strReferer & "</a> contains a broken link:" & "<br />"
End If
strEmailBody = strEmailBody & "<br />" & strQuery
'-- change the FROM and TO emails for your site
objMail.From = "webuser@example.com"
objMail.To = "administrator@example.com"
objMail.Subject = "Seo-Insights - 404 Page Not Found"
objMail.Priority = MailPriority.High
objMail.BodyFormat = MailFormat.Html
objMail.Body = strEmailBody
SmtpMail.SmtpServer = ""
SmtpMail.Send(objMail)
'-- strQuery = ""
Else
'-- switch panel displays to display the error
' when a user simply navigates to this file
pnlValid404.Visible = False
pnlNotValid404.Visible = True
End If
End Sub
</script>

<html>
<head>
<meta name="robots" content="noindex,nofollow">
<title>Seo-Insights - 404 Page Not
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</meta></title></meta></head>
<body bgcolor="#FFFFFF">
<usercontrol :Header runat="server" />
<h2>Page Not Found (404 Error)</h2>
<asp :panel id="pnlValid404" runat="server">
<p><asp :label id="lblQueryString" runat="server" ForeColor="#FF0000" /> could not be located.</p><p>
</p><p>An email has been sent to the webmaster about this problem, and action will be taken to fix it.</p>
</asp>
<asp :panel id="pnlNotValid404" runat="server" Visible="False">
<p>This page works only as a redirect when an ASPX page is not found.</p>
</asp>
<usercontrol :Footer id="myFooter" runat="server" />
</body>
</html>

Note that 2 UserControls are registered to include the header and footer, to have the same look and feel of the site. Import the System.Web.Mail namespace so we can send an email to our webmaster.
When the page loads, we can track the broken link from the URL which had triggered the error404page. When a redirect to the 404 page happens, the server appends the broken link to the error404page.
For example if the broken link was /missedpage.aspx, we can track that through Request.ServerVariables(“QUERY_STRING”). If the query is empty, probably it means the user found the missedpage on our site and browsed to it. In this case we do not want to run our code. All the code is nested inside the condition that the strQuery is not empty.
To inform the webmaster about the broken links in the site, an email will be send to the webmaster. The label control with ID lblQueryString outputs the strQuery from our code. And finally, the two panels switch content depending on what we want to show to the user. The pnlNotValid404 is set to not visible be default since this is our error display, and we only set it to visible if one occurs.

Source: http://seo-insights.blogspot.com/search/label/Error%20404%20Page

Related posts:

  1. What is Error 404 Page? How to configure Error 404 page? part 1
  2. Consuming ASP.net WebServices, WCF Services and static Page methods from JavaScript (sans MS AJAX)
  3. 10 Useful Code Snippets For PHP Developers
  4. Using database in PEAR and Smarty
  5. Start a Project with JBoss
StumbleIt!
Comments

I am very happy to see my posts in this blog. Thanks.

Please provide a link at the bottom of the post ‘Source:’ which will give me a credit.

--ReplyReply to this comment


Leave a comment

(required)

(required)


Spam protection by WP Captcha-Free