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;
Related
I am using PHP for my project and I have .htaccess file which has code below;
ErrorDoccument 404 /error-handler.php
This code is used to send or use error-handler.php file for every 404 error. Everything till here is working. But I want a file named call-404.php which when opened from browser should show 404 error and it must be handled by .htaccess file above. I tried using;
http_response_code(404);
also,
header("HTTP/1.1 404 Not Found");
All the above code does says HTTP ERROR FILE NOT FOUND but the 404 at this condition does not does as per .htaccess file. It just displays browser defined 404 error page. What is the solution for it?
And This question is not simply 404 redirection using .htaccess or simply use of http_response_code(404);. This question shows and wants solution of inability of .htaccess redirection on response to http_response_code(404).
The whole point of ErrorDoccument 404 /error-handler.php is that if NO resource is found (be it image, php script or whatever) then apache will use that directive and head to that 404 page. If however you have reached one of your own scripts, then a resource was found and therefore that .htaccess rule will not be triggered.
But you're almost there. Just header and then call the script!
<?php
header("HTTP/1.1 404 Not Found");
echo file_get_contents('/path/to/your/404page.php');
I presume that the 404 page has nothing dynamic and is just plain HTML?
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");
This isn't my exact PHP as it's rather complicated, but it's the same general idea.
if($_GET['page'] == ".htaccess"){
header("HTTP/1.0 404 Not Found");
}
When this happens, Apache doesn't load the 404 page set in the .htaccess file. I know the 404 works because when I go to a non-existent page, I get the specified 404 page.
Is there any way I can get the specified 404 page to load without manually dumping the contents of the 404 page file?
Since Apache already determined that the file actually exists, it wont look for 404 again.
One workaround could be actually sending a Location-header to a actual non-existant page and let Apache handle it. Another could be fetching the 404 page contents through PHP and outputting it together with a Status: 404-header
This may Work
if (strstr($_SERVER['REQUEST_URI'],'.htaccess')){
header('HTTP/1.0 404 Not Found');
exit();
}
I am attempting to throw my own 404 in PHP depending on certain GET vars. But the following is not working. I can confirm that the pge header is coming back with a '404 status code' though. .htaccess just doesn't seem to be redirecting correctly. Am I missing something?
PHP Code:
if(!$_GET['page']){
header('HTTP/1.0 404 Not Found');
}
.htaccess Code:
ErrorDocument 404 /404.html
Many thanks!
As far as Apache's concerned, it's done its job as it has properly found the page/script that the user's request called for. The fact that the script is outputting a 404 header is irrelevant to Apache, since its job was completed properly.
You'd need to have your PHP script output the 404 header, and include the page the Apache 404 handler points at it:
if (!$_GET['page']) {
header('HTTP/1.0 404 Not Found');
include('404.html');
exit();
}
Don't do a redirect to the 404 page, as that'd just turn the hit into a normal GET request, with a 200 OK result.
You cannot make the error page start with a number on a XAMPP test server I know...
It may be true for windows ... period.
that one eluded me for hours!
Try renaming your error page to:
error404.html
then change your .htaccess Error section for your 404 to:
ErrorDocument 404 /error404.html
And don't forget to start it with the forward slash. That will fix it.
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!