404 Error — Page Not Found

Create Custom 404 Error Page in PHP and Apache Using .htaccess

Before creating a custom 404 page not found error page we should understand what is 404 page not found error code? The 404 code is a standard HTTP response code that tells the client browser that the server is communicable but could not find the requested resource. When the requested resource is not found on the server then server sends a 404 code in response of the client’s request. This is also called page not found error. A 404 response simply means the server has not found anything matching the Request-URI. It does not indicate if the requested resource is unavailable temporarily or permanently. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.

Why is Custom 404 Error Page Needed?

User is shown a 404 error page when he/she tries to access a non-existent page on a site that has either been moved or deleted or user has mistyped a URL. A 404 error page is just an information for the user that the page requested by him/her is not available. This is site owner’s responsibility to return a meaningful and well formatted 404 error page so that surfer gets useful information when the requested page is not available.

Читайте также:  Php post data example

Let’s Start — Create Custom 404 Error Page

To create a PHP version of custom 404 error page open any text editor of your choice and insert the following code and save it as 404page.php or any other name of your choice but the extension must be .php because here 404 error page is being demonstrated with PHP technology.

     404 Error - Page Not Found!  

Following things you must remember while creating a 404 error page.

  • You must specify the 404 HTTP response header in your custom 404 error page else it would be treated as a normal page and 200 OK response will be sent to client browser.
  • The header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. In above sample error page the header that starts with the string «HTTP/» (case is not significant) will be used to figure out the HTTP status code to send. Here we have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive — will be explained shortly), we make sure that our script generates the proper status code.

The above page is just for demonstration, you can beautify your custom 404 error page as you like and add more relevant content depending upon how much information you want to convey to your audience.

Put the Custom 404 Error Page into ROOT Directory of Your Website

After having created a custom 404 error page you have to put this page into home or ROOT directory of your website. To do that connect to your website by ftp client or some file manager tool and upload the error page there.

Читайте также:  Php autoload using namespaces

Create/Modify the .htaccess File

Those who are beginners the .htaccess file is a file found in Apache servers that allows you to manipulate the behaviour of the server. A very common use of .htaccess is to create custom error messages for server errors. This article configures error page on Apache web server. If your web host is not running on Apache then this solution may not work for you; and in that situation you will have to find out from your web host what you need to do to enable the server to serve your customized file when a web page is not found.

If your website running Apache web server, the first thing you need to do is to add the following line to a file named .htaccess (note the preceding period). In most instances, no such file will exist, and you can simply create one with a text editor (such as Notepad on Windows). Incidentally, if you are using Notepad on Windows, you will need to enter «.htaccess» (including the opening and closing quotation marks) into the «Save As» dialog box when you create the file, otherwise Notepad will append a «.txt» extension behind your back when it creates the file. And unless you configured Windows to show you the full filename, you won’t even realise that it has done so.

ErrorDocument 404 /404page.php

Now, upload your .htaccess to the root directory of your website. By doing above steps you are done with 404 error page and this is the time to test the error page.

Testing the 404 Error Document

Test your 404 error page by typing a URL that you know does not exist. Your error page should load up. Remember the domain name must be correct followed by a wrong page name. If domain name is not correct the request won’t reach to your website.

For the custom 404 error page you must check if that returns a correct 404 response. If your page still returns 200 OK or 302 response code then something wrong there and your page won’t serve the purpose it was designed for. Chrome’s inspect element and Firefox’s firebug extension may help you in checking response headers for your error page.

Does Custom 404 Error Page Return 200 OK or 302 response?

For custom 404 error page, the path given in .htaccess must be relative to ROOT directory else your page will return a 200 OK response, which will of course won’t serve the desired purpose. This fact applies to almost all servers including both Apache and IIS. For example, see the following entries and accommodate the correct one in your .htaccess file. The second one having absolute URL for error document will return 200 OK response.

ErrorDocument 404 /404page.php #Correct implementation of error page 
ErrorDocument 404 http://cs-fundamentals.com/404page.php #WRONG! Results in 200 OK 

Last Word

Hope you have enjoyed reading this tutorial on creating custom 404 page not found error page. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

Источник

Apache Errordocument 404 not working – Here’s the quick fix

By default, Apache comes with a set of standard error documents. They show up when the requested website page do not exist.

Often these response pages can be intimidating to site users. That’s where custom error-document helps. But, it needs additional configuration and often result in Apache error-document 404 not working message.

And, Bobcares’ Engineers fix Apache error-document settings as part of our Server Administration Services.

Today, we’ll see how we setup and fix Apache error-document 404 error.

Why custom Apache error-document ?

Firstly, let’s check on why we need a custom error document in Apache web server.

Website owners focus on user friendliness in every web page. As a result, they may wish to provide custom error responses in their website.

Luckily, Apache helps to set custom error document using the ErrorDocument directive. Users can set this for the entire server globally, for a single website, or in directory context. For site specific settings, it can be set in .htaccess file.

And, it allows to include custom error page that fits to the site layout.

The syntax of the ErrorDocument directive is:

For example, in real servers it looks like

Here, 404.php is the custom designed web page.

What causes 404 error in custom error-document?

Now, its time to check the various causes that result in custom error document failure.After setting the custom error page, if the page is not accessible, it end up in a ‘Page not found‘ aka 404 error.

From our experience in managing Apache servers, the major reasons for Apache Errordocument 404 error are:

1. Wrong path

Unfortunately, a major share of problems with custom error-document happens due to wrong path setting of the error page. For instance, when the custom error page 404.php reside inside a sub folder, calling it by the path /404.php results in error. In short, relative path may create problems. Therefore, path should be specified as the absolute path to your exact file. On a cPanel server, this would be something like /home/user/public_html/error_pages/404.php

2. Incorrect website settings

Similarly, even after designing custom error pages, some customers forget to enable it on the domain. This typically happens in servers with control panel. For example, in Plesk servers, improper setting of Custom error documents in Hosting Settings can cause trouble.

3. Browser error page settings

Again, turning on “friendly” error page in browsers can also be a reason for Apache Errordocument 404 not working.

For instance, “Friendly HTTP Error Message” is a feature that is enabled by default in Internet Explorer browser. If the server response to some common errors like 404 is less than 512 Bytes, then IE’s “friendly” error page is displayed instead of custom error page.

How we fix custom error-document for website

When customers report problems with custom error pages, our Dedicated Engineers follow a step by step procedure to figure out the problem. Let’s have a look at the exact steps.

    1. Check the direct link of the custom 404 page. When that is accessible, we rule out the possible coding problems with the custom web page. This means the problem lies with the configuration.
    2. We, then check the ErrorDocument settings in .htaccess file. Or ,if it is set in httpd.conf, we verify and correct the VirtualHost settings of the domain.
    3. Next, we set the full path to the error page in .htaccess file. Again, in servers with control panels like Plesk, we set the option correctly as shown below.

    Dedicated Engineers advise customers to turn off Internet Explorer’s friendly error message. For this, in Internet Explorer, Under Internet Options > Advanced > Browsing, uncheck the “Show friendly HTTP error messages” option.

    [Having trouble with Apache ErrorDocument? We can fix it for you.]

    Conclusion

    In short, Apache Errordocument 404 not working error mainly happens due to wrong path setting, incorrect Apache configuration and many more. Today, we saw the typical reasons for the error and how our Support Engineers fix and make custom error page working.

    PREVENT YOUR SERVER FROM CRASHING!

    Never again lose customers to poor server speed! Let us help you.

    Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

    Источник

Оцените статью