This tutorial will help us in learning how to create error pages in apache. We can customize the error pages based on different error codes that is writen in Apache server's Directive ErrorDocument
Follwoing are the Three digit error code:
Apache error codes (401, 403, 404, 412, 500)
If we come accross an error , we can handle it in four ways as below:
- Display a simple hardcoded error message
- Display a customized message
- Handle the problem by redirecting to an external URL
- Handle the problem by redirecting internally a local URL-path
Note: The first option is default one and the 2-3 options are configured using the ErrorDocument Directive.
Let us create a custom error page for error code 404 (i.e. file not found error)
Step 1: First write an html page, say index.html to display the error message
Step 2: Now place this file inside the site's root directory
For example : path is = /var/www/html/projectrootdirectory/index.html
if we place this index.html file in some other subdirectory say Error
path = /var/www/html/projectrootdirectory/Error/index.html
Step 3: Now include apache server's directive ErrorDocument in the .htaccess file
with the syntax as below:
ErrorDocument error-code action
1. ErrorDocument : it is the Directive
2. error-code : (i.e. any of the 401, 403, 404, 412, 500)
3. action : the path of your custom error page.
Note: for default action we can just write a message in a single quotes
OR
Find your Web server configuration file i.e. httpd.conf.
In case of apache server your path will be
/etc/apache2/apache2.conf
For Example: We can create custom error page for a site under following path
/etc/apache2/sites-enabled/000-default.conf
Below is the code that we will write in the 000-default.conf file
<VirtualHost *:80>
ServerAdmin admin@firstexample.com
ServerName firstexample.com
ServerAlias www.example.com
DocumentRoot /var/www/firstexample/public_html
ErrorDocument 404 /ErrorPages/404pagenotfound.html
# Other directives here
</VirtualHost>
Similarly we can maintain custom errror pages for the following error code 401, 403, 412, 500
For more information regarding apache custom error pages click the Link below.
http://httpd.apache.org/docs/trunk/mod/core.html
0 Comment(s)