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.
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?
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 have RewriteRule /somefile.php index.php [L] in my .htaccess
and header("HTTP/1.0 404 Not Found"); in my index.php.
But 404 don't work, and I getting blank page. Why?
header("HTTP/1.0 404 Not Found"); works without htaccess file.
Don't ask me why I doing like this. :D
Probably your php is a fastcgi, then you need to do header("Status: 404 Not Found") instead. Or your headers are already sent, in which case you may need to set output buffering for php. And check your logs ;-)
in phpinfo I have Server API CGI/FastCGI.
and I discovered that header("Status: 404 Not Found") working only in IE. In Chrome/Firefox I see only blank page.
some browsers also show custom error messages if they not more than 256 or 512 characters
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!
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.