All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Custom Error Page Using PHP

Last Updated : Jul 1, 2023

IN - PHP Htaccess | Written & Updated By - Pragati

In this tutorial we will show you how to create custom error page using PHP, when someone visit a page in your website and if the page is not found or the page is forbidden and or the webpage encountered internal server error than a default server webpage will be display which is not customisable and visually beautiful.

Custom Error Page Using PHP

To Create Custom Error Page It Takes Only Two Steps:-

  1. Create or modify .htaccess file and write redirect method
  2. Make a HTML file and define markup and styling

Step 1. Create or modify .htaccess file and write redirect method

#Rewrite URL's
RewriteEngine On
RewriteRule ^error/?$ errors/error.html [NC]

# Enable Error Documents
# (404,File Not Found) | (403,Forbidden) | (500,Internal Server Error)
ErrorDocument 404 /error
ErrorDocument 403 /error

In this step we create a .htaccess file in server main directory and write some rules for error page.

We turn the rewrite engine on so we can redirect url to our custom error page then we define rules for 404 (File Not Found), 403 (Forbidden) and 500 (Internal Server Error) webpage to redirect when any of these error occur to 'error.html' page which we were going to create in next step.

Step 2. Make a HTML file and define markup and styling

We make a HTML file and save it with a name error.html

<html>
<head>
<style>
body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 background-color:#01A9DB;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#wrapper h1
{
 margin-top:50px;
 font-size:65px;
}
#wrapper a
{
 color:blue;
 font-size:20px;
}
</style>
</head>
<body>
<div id="wrapper">

<h1>Sorry For The Inconvenience Please Try Again Later</h1>
<a href="http://talkerscode.com">TalkersCode.com</a>

</div>
</body>
</html>

In this step we create a simple page which is displayed on any of these errors written above.

That's all, this is how to create custom error page using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on custom 404 page and other error pages php helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪