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');
Related
I have this 404 redirection in my .htaccess file :
ErrorDocument 404 /404.php
It works fine when I try to access a page that doesn't really exist on my server.
The problem is that when I return my own HTTP 404 status in a page that really exists on the server, it doesn't redirect me to the 404.php page, but the browser displays a standard 404 error.
I return my own HTTP 404 status when I have a wrong url parameter for example.
Can you help me to define the rule in the .htaccess file, please?
in your .htaccess file you should have this rule
RewriteEngine On
ErrorDocument 404 /404.php
and if you want to simulate this error in anyplace other than typing it manually in the header you should use http_response_code
PHP code
<?php
http_response_code(404);
include('my_404.php'); // provide your own HTML for the error page
die();
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");
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
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;
}
?>
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');