Forum Moderators: phranque
What I would like to do, in the case of an invalid url, is:
1. Return a 404 status e.g. header("HTTP/1.0 404 Not Found");
2. Redirect the browser to the homepage
3. Stop any futher processing!
My understanding is that I can send a header("Location: [example.com...] but this results in a 302 status. I'm fairly sure I don't want a 302 status (or a 301 for that matter), as the page being requested never existed - so it should be a 404 error. I'm also a little wary of Google then seeing my homepage content under multiple pages and getting hit for duplicate content!
Any tips on how to redirect but keep the 404 status, or whether I should just use a 301?
Better to have a custom 404 page that
Okay, in which case, how do I redirect the browser to this custom 404 page? For some reason, just using
header("HTTP/1.0 404 Not Found");
doesn't seem to actually send the browser to any 404 page at the moment!
I'd also better check if I can have a custom 404 page...though I guess if I can redirect, then it can be to a page of my own choosing?
ErrorDocument 404 http://www.example.com/errorpages/404.html
The URL should be the URL of your 404 error page. Make sure you think seriously about the design of this page. From what you say, it could be important to you.
You should probably also have the following in your .htaccess file, in order to catch other standard erors:
ErrorDocument 401 http://www.example.com/errorpages/401.html
ErrorDocument 403 http://www.example.com/errorpages/403.html
ErrorDocument 500 http://www.example.com/errorpages/500.html
if $path exists
then generate page
else throw 404 error
However, using the php heder function - header("http/1.0 404 Not Found") - for that last part doesn't appear to force a 404 error.
Reading the php site on this topic, it might be interpreted as saying that this directive would appear *ON* the 404 page to indicate that it is the 404 error page, rather than being used to force a 404 error in the first place. But that interpretion is open to question, I'm not clear...
So, I'm back to the original question - how do I force a 404 error, using php?!?
By using PHP to send the header, I'm in fact bypassing Apache, which means I have to generate the page within the php itself. I.e.
if $path exists
then generate page
else generate 404 errorpage
Using the header() function to signal to indicate that it is a 404 page, of course!