My .htaccess redirects all requests to /word_here
to /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?
Immediately after that line try closing the response using
exit
ordie()
or
Try this:
Create custom error pages through .htaccess file
1. 404 - page not found
2. 500 - Internal Server Error
3. 403 - Forbidden
4. 400 - Bad request
5. 401 - Authorization Required
You can also redirect all error to single page. like
try putting
in
.htaccess
file.Do this for any error but substitute 404 for your error.
The up-to-date answer (as of PHP 5.4 or newer) for generating 404 pages is to use
http_response_code
: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.:
You could configure Apache to use the same page for its own 404 messages, by putting this in httpd.conf: