Custom 404 Error Page in PHP - 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';
}

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

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.

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

add this line in .htaccess
ErrorDocument 404 "Page not found."

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.

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

Related

apache conf errordocument not working for declared 404 header in php

I am issuing a 404 in my coupon.php page:
<?php
$id=$_GET['cid'];
$rs=mysqli_fetch_assoc(mysqli_query($scx_dbh,"select * from locations where locid=$id"));
if($rs==NULL){
header("HTTP/1.0 404 Not Found");
}
Inside my apache config httpd.conf I have the following declared:
ErrorDocument 404 /404.html
However when i go to the page that issues the 404 it does not load the error document but just shows a browser error stating a 404. It only seems to load when a page does not exist, not when i issue a 404 in php.
Any help would be appreciated
Sending a 404 error using header("HTTP/1.0 404 Not Found"); will only tell the end browser that there was an error finding this page. It won't show your custom error page.
If you want to show the end user an error page, you will need to include or redirect to it.
Place either of the two below options under your 404 header.
Include Option
include('/path/to/404.html);
Redirect Option
header("Location: /path/to/404.html");

Track links that causes 404

I am trying to track all those link that causes 404.i tried $_SERVER['HTTP_REFERER'] on 404 page but it doesn't work for me .
NOTE: i don't wanna use google analytic API. Is there any other possible way
Create a .htaccess file that will redirect to a 404 page on 404 error.
Add this code to your .htaccess file: ErrorDocument 404 /404.php and in the root of your code create a file called 404.php. In that 404.php file you can do all the handling of the error.
$_SERVER['REQUEST_URI'] should have the URL that was requested in the browser.
Also make sure that you are loading the 404 page and not redirecting to the 404 page.

Display 404 document from htaccess after sending 404 header in PHP

In PHP I use the following code to indicate page not found:
header("HTTP/1.0 404 Not Found");exit();
In .htaccess file I have the following line to handle 404 custom document:
ErrorDocument 404 /404.html
Now when I send the PHP 404 header I expect it to show the custom 404.html defined in .htaccess but instead it shows a message "this link appears to be broken". So it doesn't show the custom 404 page and if I remove the line from .htaccess it still won't display the regular 404 page of the server and shows also the broken link message.
So how to make it show the 404 page when I send the header in PHP?
The problem here is that the custom error document denoted by ErrorDocument isn't processed unless the URL processing pipeline can't find a file (or a handler) that the URL points to. In you case, the processing pipeline does find a file, your php file, thus it never reaches a 404 on its own so the ErrorDocument is never applied.
When apache uses the php handler to run your php file, and your php file decides to return a 404, you not only need to provide the 404 response code, but the HTML error document as well. You don't see the default apache error document because apache never thinks the request is a 404 (it handed the request off to your php file). So you need to do something like this:
header("HTTP/1.0 404 Not Found");
include('404.html');
exit();
you just send a 404 code to your browser try to redirect to your error 404 page
header('location: http://youdomain.com/404.html'); exit;

Problem with apache mod_rewrite and 404 pages

I have a custom 404 error page, and a mod_rewrite rule. If I access a page that does not exist, I get my 404 error page. My problem is that if I issue a 404 header from my php page, it does not open my 404 page, instead, I get this:
Not Found
The requested URL /index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying
to use an ErrorDocument to handle the request.
This is my .htaccess:
RewriteEngine on
ErrorDocument 404 /errors/404.php
RewriteRule ^[A-Za-z0-9]{8}/$ /index.php
This is my redirect from /index.php that will 404 only if the key does not exist. The $key is obtained from parsing the URL (e.g. http://localhost/aKeYCoDE/):
<?php
if (!key_exists($key)){
header('HTTP/1.0 404 Not Found');
exit;
}
?>
I am expecting it to redirect to my 404 page.
UPDATE:
It is definitely something about the fact that I am calling 404 from a page that was rewritten (/index.php). If I create a dummy page: /redirect.php, and then do nothing but issue the 404 from there, I get my custom 404 page. But, if I write a mod_rewrite rule for it, and try to access it that way, I get the default 404 error page.
I found the answer - the problem was my assumption that a header 404 from php would redirect to a 404 page. That was wrong. The Apache server can issue 404 for pages that do not exist, but for pages that do exist, i.e., are being served by the php page, the 404 response goes to the browser.
This thread was of a similar issue:
http://www.webmasterworld.com/forum88/10955.htm
To make php do what I want it to do (issue the 404 when key does not exist), I need to include the 404 page from php:
<?php
if (!key_exists($key)){
include($_SERVER["DOCUMENT_ROOT"]."/errors/404.php");
exit;
}
?>

catching all invalid URLs

I recently upgraded a site and almost all URLs have changed. I have redirected all of them (or so I hope) but it may be possible that some of them have slipped by me. Is there a way to somehow catch all invalid URLs and send the user to a certain page and somehow know what URL the person came from so I could log this, and fix those? I'm thinking I could use .htaccess somehow but am not sure how. I am using PHP Thanks so much!
You could use a custom ErrorDocument handler written in PHP to catch URLs having "slipped by":
# .htaccess file
ErrorDocument 404 /not-found.php
And in not-found.php:
switch($_SERVER['REDIRECT_URL']) {
case '/really_old_page.php':
header('HTTP/1.1 301 Moved Permanently');
header('Location: /new-url/...');
/* As suggested in the comment, exit here.
Additional output might not be handled well and
provokes undefined behavior. */
exit;
default:
header('HTTP/1.1 404 Not Found');
die('404 - Not Found');
}
in .htaccess in the web root
ErrorDocument 404 /yourFile.php
Easiest would be to add a custom 404 redirect in htaccess.
Just add something like this:
ErrorDocument 404 /errors/404.php
to a .htaccess (create one if needed) in your application root. And all request to none extistent pages will be redircted to your own custom 404 page.

Categories