Search code examples
php.htaccesshttp-status-code-404

Custom 404 Error Page in PHP


I am a novice in PHP, I hope you can help me with this problem. How can I create a custom 404 error page in PHP without going anywhere or redirect it to other page.

Ex. mysite.com/no-where

The page no-where does not exists. I want to display a 404 error message with design on that URL.

How can I get the current URL? I got an error on this. Coz I want to check the header if its 404.

$url = $_SERVER['REQUEST_URI'];
$array = get_headers($url);
$string = $array[0];
if(strpos($string,"200")) {
    echo 'url exists';
} else {
    echo 'url does not exist';
}

Solution

  • Create a .htaccess file and put the following line in it:

    ErrorDocument 404 /errorpageFileName.php 
    

    That will set the page 'errorpageFileName.php to your error 404 page. You can of course change the file name to your likings.

    You can read more in-depth about it here: Custom 404 error issues with Apache