All TalkersCode Topics

Follow TalkersCode On Social Media - 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

 margin:0 auto;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 margin:0 auto;
#wrapper h1
#wrapper a
<div id="wrapper">

<h1>Sorry For The Inconvenience Please Try Again Later</h1>
<a href=""></a>


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 🡪