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
Related
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');
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!
If the ID (ie user) does not exist I send header("Status: 404 Not Found"); to the browser but PHP sends rest of the page too. Then I made it to send nothing but then it showed a blank white page. I want it to show the default error page. How I'll let it to do it?
Add die; after the header call as well.
you can set it via htaccess also,
ErrorDocument 500 http://foo.example.com/500.php
ErrorDocument 404 /404.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
depending on http status response
header("Status: 404 Not Found");
readfile ($_SERVER['DOCUMENT_ROOT']."/404.html");
exit;
?
or you need universal solution?
I think that in this case, if you only set the header, you'll get the default error page of your browser and not of your web server. The problem is that the page exists for the webserver. So if you know where your server's error page is stored, read it from there. And make sure that your response is longer than (I think it was) 512 Bytes, otherwise the browser (especially IE) will give you it's very own error page.
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');