I’m trying to show an HTTP error document from a PHP page, but I would like the original URL to remain in the address bar to prevent search engine crawlers getting confused and to allow for reloading of the page in case it’s a temporary issue.
I made a redirect function in PHP which goes a bit like this:
public static function Redirect($url, $code = '303 See Other') {
header('HTTP/1.1 ' . $code);
header('Location: ' . $url);
exit(0);
}
If I want to display an error document, such as 403 Forbidden, I would do the following: Redirection::Redirect('/errordocs/403.php', '403 Forbidden'); and it would work fine.
As I said though, the users URL will change to /errordocs/403.php which I want to avoid.
What I did try to do was remove the header('Location: ' . $url); line if the HTTP code was 4xx or 5xx. I was hoping this would then trigger Apache to display the correct document as I have my .htaccess set up to point to the relevant error pages (which works fine as it is).
What I actually got from doing this was the standard Google Chrome messages for when stuff breaks rather than my pretty custom error documents.
In a quick consensus, what’s the best way of doing this now? Making it echo the page instead of redirecting?
You can just display the contents of the error html in case of an 4xx status code. Otherwise redirect:
}
The above example will send proper HTTP status code, will display the contents of the error page and keeps your url in the address the same.