Create a custom 404 Error Page in php using htaccess

By Guest | Nov 10, 2021 | PHP
Share : Whatsapp

https://www.fundaofwebit.com/post/create-a-custom-404-error-page-in-php-using-htaccess

How to Create a custom 404 Error Page in PHP using htaccess


In this tutorial, you will learn how to create a 404 error page in php using htaccess.

We will be using this 404 Error Page in websites. When the requested url or page is not found, we will display the 404 Error Page, which we will be doing in php using htaccess file. So, Let's get started:

We will be using Bootstrap v5 to design the 404 Error Page.

Step 1: Create a file named 404.php and paste the below given design:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>404! Page Not Found</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" >
</head>
<body>

    <div class="py-5">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-7">
                    <div class="card card-body text-center">
                        <h1>404!</h1>
                        <h4>Page Not Found!</h4>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>

Step 2: Create dot . htaccess file in your root folder and paste the below code:

RewriteEngine On

ErrorDocument 404 http://www.yourdomain.com/404.php

and now, lets try to access wrong page or url as https://www.yourdomain.com/testing-wrong-url.php

your will be redirected to your 404.php page.

Thanks for reading.