PHP 404 handling - php

All requests go through index.php, and if content was not found I do:
header("HTTP/1.0 404 Not Found");
exit;
and expect to see contents of /404 as defined in .htaccess: ErrorDocument 404 /404
The problem is that I see a blank page on Chrome and Firefox, but on IE see its 404 page (not mine, browsers 404 page).
Sending header is not enough to handle redirect, so it's expected to be done by .htaccess, but fails. Should I redirect it whith PHP like so:
header("HTTP/1.0 404 Not Found");
header("Location: " . $dirpath . "404");

No ErrorDocument 404 won't work on your way out from PHP. That is only applicable when Apache detects 404 for an incoming request and ends up invoking ErrorDocument 404 handler.
Once control is handed over to PHP as normal request processor Apache just returns output returned by PHP module to a requesting client.
Only thing you can do is this:
require_once("404.php"); // include 404 handler
exit;
And inside 404.php you can do:
http_response_code(404); // sends 404 status to browser

Related

Force 404 in browser [duplicate]

My file .htaccess handles all requests from /word_here to my internal endpoint /page.php?name=word_here. The PHP script then checks if the requested page is in its array of pages.
If not, how can I simulate an error 404?
I tried this, but it didn't result in my 404 page configured via ErrorDocument in the .htaccess showing up.
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
Am I right in thinking that it's wrong to redirect to my error 404 page?
The up-to-date answer (as of PHP 5.4 or newer) for generating 404 pages is to use http_response_code:
<?php
http_response_code(404);
include('my_404.php'); // provide your own HTML for the error page
die();
die() is not strictly necessary, but it makes sure that you don't continue the normal execution.
What you're doing will work, and the browser will receive a 404 code. What it won't do is display the "not found" page that you might be expecting, e.g.:
Not Found
The requested URL /test.php was not found on this server.
That's because the web server doesn't send that page when PHP returns a 404 code (at least Apache doesn't). PHP is responsible for sending all its own output. So if you want a similar page, you'll have to send the HTML yourself, e.g.:
<?php
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);
include("notFound.php");
?>
You could configure Apache to use the same page for its own 404 messages, by putting this in httpd.conf:
ErrorDocument 404 /notFound.php
Try this:
<?php
header("HTTP/1.0 404 Not Found");
?>
Create custom error pages through .htaccess file
1. 404 - page not found
RewriteEngine On
ErrorDocument 404 /404.html
2. 500 - Internal Server Error
RewriteEngine On
ErrorDocument 500 /500.html
3. 403 - Forbidden
RewriteEngine On
ErrorDocument 403 /403.html
4. 400 - Bad request
RewriteEngine On
ErrorDocument 400 /400.html
5. 401 - Authorization Required
RewriteEngine On
ErrorDocument 401 /401.html
You can also redirect all error to single page. like
RewriteEngine On
ErrorDocument 404 /404.html
ErrorDocument 500 /404.html
ErrorDocument 403 /404.html
ErrorDocument 400 /404.html
ErrorDocument 401 /401.html
Did you remember to die() after sending the header? The 404 header doesn't automatically stop processing, so it may appear not to have done anything if there is further processing happening.
It's not good to REDIRECT to your 404 page, but you can INCLUDE the content from it with no problem. That way, you have a page that properly sends a 404 status from the correct URL, but it also has your "what are you looking for?" page for the human reader.
try putting
ErrorDocument 404 /(root directory)/(error file)
in .htaccess file.
Do this for any error but substitute 404 for your error.
In the Drupal or Wordpress CMS (and likely others), if you are trying to make some custom php code appear not to exist (unless some condition is met), the following works well by making the CMS's 404 handler take over:
<?php
if(condition){
do stuff;
} else {
include('index.php');
}
?>
Immediately after that line try closing the response using exit or die()
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
exit;
or
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
die();
try this once.
$wp_query->set_404();
status_header(404);
get_template_part('404');

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");

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;

Problems with 404 redirect with apache and php

I have a site which, for a long time, redirect the user directly to the hompage if the url was incorrect.
Instead of returning 404 error, the script used header('location: /'); die(); .
I've changed that line to header("HTTP/1.0 404 Not Found");die(); and the server started to send the 404 error message.
But, now I get the browser 404 error and not my custom 404 page.
So, I've opened .htaccess file and added
ErrorDocument 404 /index.php
But no success, I'm still getting the browser 404 page and not my 404 page
(I've also tried to set it under /etc/apache2/sites-available/site-conf)
Also tried:
$_SERVER['REDIRECT_STATUS'] = 404;
header("HTTP/1.1 404 Not Found");
header('location: /');
die();
And still - no success
Any Ideas?
header("HTTP/1.1 404 Not Found");
include('index.php'); // maybe you have to adjust the path;
exit;
When you use header(location) you are setting the status to 3xx. And if the status is 404, browser does not need to follow any location headers. It should follow the location header only in 3xx responses.
you are mixing some things.
when your script runs, this means that there is a page.
within your script you can control the status via header("HTTP/1.1 404 Not Found");
this say the browser "not found".
finally a page is served with a 404 status. all output of that page is the errorpage. everything is done by your script.
when you are define within a htaccess file a 404 error page, then this page is display if the apache server handles the not found error. the means your browser has to access a file which is not present on the server. then the file index.php is displayed.
header status and header location cannot used together.
a location command sets the status to a redirect code 3xx. after redirection the server serves the page with code 200.
what you can do is, redirect the client to a random url on your server.
then the server is displaying the 404 page.
First check if .htaccess files are enabled in your Apache configuration (AllowOverride directive). The ErrorDocument you have tried should work!

How to create an error 404 page using PHP?

My file .htaccess handles all requests from /word_here to my internal endpoint /page.php?name=word_here. The PHP script then checks if the requested page is in its array of pages.
If not, how can I simulate an error 404?
I tried this, but it didn't result in my 404 page configured via ErrorDocument in the .htaccess showing up.
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
Am I right in thinking that it's wrong to redirect to my error 404 page?
The up-to-date answer (as of PHP 5.4 or newer) for generating 404 pages is to use http_response_code:
<?php
http_response_code(404);
include('my_404.php'); // provide your own HTML for the error page
die();
die() is not strictly necessary, but it makes sure that you don't continue the normal execution.
What you're doing will work, and the browser will receive a 404 code. What it won't do is display the "not found" page that you might be expecting, e.g.:
Not Found
The requested URL /test.php was not found on this server.
That's because the web server doesn't send that page when PHP returns a 404 code (at least Apache doesn't). PHP is responsible for sending all its own output. So if you want a similar page, you'll have to send the HTML yourself, e.g.:
<?php
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);
include("notFound.php");
?>
You could configure Apache to use the same page for its own 404 messages, by putting this in httpd.conf:
ErrorDocument 404 /notFound.php
Try this:
<?php
header("HTTP/1.0 404 Not Found");
?>
Create custom error pages through .htaccess file
1. 404 - page not found
RewriteEngine On
ErrorDocument 404 /404.html
2. 500 - Internal Server Error
RewriteEngine On
ErrorDocument 500 /500.html
3. 403 - Forbidden
RewriteEngine On
ErrorDocument 403 /403.html
4. 400 - Bad request
RewriteEngine On
ErrorDocument 400 /400.html
5. 401 - Authorization Required
RewriteEngine On
ErrorDocument 401 /401.html
You can also redirect all error to single page. like
RewriteEngine On
ErrorDocument 404 /404.html
ErrorDocument 500 /404.html
ErrorDocument 403 /404.html
ErrorDocument 400 /404.html
ErrorDocument 401 /401.html
Did you remember to die() after sending the header? The 404 header doesn't automatically stop processing, so it may appear not to have done anything if there is further processing happening.
It's not good to REDIRECT to your 404 page, but you can INCLUDE the content from it with no problem. That way, you have a page that properly sends a 404 status from the correct URL, but it also has your "what are you looking for?" page for the human reader.
try putting
ErrorDocument 404 /(root directory)/(error file)
in .htaccess file.
Do this for any error but substitute 404 for your error.
In the Drupal or Wordpress CMS (and likely others), if you are trying to make some custom php code appear not to exist (unless some condition is met), the following works well by making the CMS's 404 handler take over:
<?php
if(condition){
do stuff;
} else {
include('index.php');
}
?>
Immediately after that line try closing the response using exit or die()
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
exit;
or
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
die();
try this once.
$wp_query->set_404();
status_header(404);
get_template_part('404');

Categories