user3745440
user3745440

Reputation: 53

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';
}

Upvotes: 4

Views: 17367

Answers (6)

Hans Ganteng
Hans Ganteng

Reputation: 189

If you are using apache as a web server and cpanel, just click menu error page, choose 404 error not found and edit the editor with your custom html page and save. You will get automatic file 404.shtml in your folder.

If you are using vps openlitespeed and webadmin panel, there is error not found and fill with your custom 404 html page.

If your panel is cyberpanel at openlitespeed, just create your custom 404 html page (ex:404.html), then edit your vHost Conf and fill with line:

errorpage 404 {
  url                     /404.html
}

And save it

Upvotes: 0

Johnny
Johnny

Reputation: 15423

It's really important to have a correct 404 error page, because:

  • It helps your site / webapp visitors to guide for the correct place in your site
  • Much better SEO
  • It's just looks better

In additional the the answers here, I would suggest you to have different messages for different error types. For example, define in your .htacess file an error page for different failure types:

ErrorDocument 500 /500.html
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html

More information about custom 404 error pages.

Upvotes: 2

Vinay
Vinay

Reputation: 2339

add this line in .htaccess

ErrorDocument 404 "Page not found."

Upvotes: 1

pradip kor
pradip kor

Reputation: 459

If you are using apache as a web server you can create a file named .htaccess in the root directory of your website. This can contain various rules, including error handling, as follows:

ErrorDocument 404 (Your site error 404 page URL)

For example:

ErrorDocument 404 http://example.domain/custom-404-page.html

Upvotes: 1

TrickyInt
TrickyInt

Reputation: 157

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

Upvotes: 8

user399666
user399666

Reputation: 19909

Use a 404 header and then require your 404 template / page.

header('HTTP/1.0 404 Not Found');
include '404.php';
exit;

That or you can configure Apache to serve up a custom page.

Upvotes: 0

Related Questions