Redirect 404 error page
Created: Last updated:
Lately I see links to one of my other pages for questions like 404 page error or how to redirect 404 in PHP or Zend.
Unfortunately that webpage is not specifically about 404 redirects so let me address this questions here about 404 error pages in PHP or Zend.
Redirect versus Error page
We have to clarify one thing. If you came to this page for precisely questions like 404 redirect or how to redirect to 404 error page then I see it as my obligation to tell you that there is no such thing as a 404 redirect.
What is a redirect 3xx
A redirect means to send a special message to a browser to look for another document with another link. A redirect tells a browser explicitly to look for the request somewhere else, i.e. you have to return a new link to the browser. A redirect requires a 3xx status codes which tells a browser to load the page with the new link.
Furthermore, for a redirect you don't have to return a HTML document because the browser should terminate the current request and then load and render the document with the submitted link. With php no HTML document will be submitted and therefore ever be seen.
If your web application has decided to send a redirect and added all the required header data it can be terminated immediately.
For more information see my other document for how to actually use redirect in the Zend Framework and also PHP.
What is an error 4xx
Now, error pages are slightly different. The only thing similar is that we also add special information to the header which is then returned to the client's browser. For an error we have to use one of 4xx status codes.
When we return a 4xx status code one immediate difference compared to a redirect is that any of these codes instructs the browser to do—nothing!
A browser doesn't do much it seems. However, all caching systems in between and the browser will act differently in terms of how to save the document.
Depending on the error certain browsers may not even send a new request when a user refreshes the page. It simply reloads the error page from its cache. You may see that in your server logs, i.e. you don't see any new requests no matter how often you hit F5 in your browser. With some error codes you told the browser the document is gone, so why bother even trying again. Another result is Google and other search engines who will not index such a page.
The other and main difference to a redirect is that you will have to craft and return an HTML document.
How to send 4xx error
Finally the answer to how to send an 4xx error?
First you must know or decide what type of error code. The official and technical source is W3C Error codes list. There is also a Wikipedia page with a HTTP status codes page.
A special note here: The redirect and error codes are actually called Status Codes and belong to the HTTP specification. Although you use them for your HTML webpages they are not part of an HTML document.
This special note immediately points out a very important fact. You cannot add the codes anywhere in your HTML document. They have to go into the header of the response which has nothing to do with the <head> section of the HTML document.
Header information in PHP
We can set header information with the aptly named header() function in PHP. See the manual for details.
Header information in Zend Framework
Just to make this clear, the Zend Framework doesn't do anything different. The helper class does exactly the same and uses the header() PHP function.
- $error_404 = 'HTTP/1.1 404 Not Found';
- $this->getResponse()->setRawHeader($error_404);
Note that has to go inside your controller.
Final warning
Just remember and look out for two things.
First: You have to see all header information before you echo anything. Anything means also no empty spaces which some can happen accidentally when you have a simple space or even a empty line before <?php opening tag. The same can be true at the end, i.e. a simple space or a newline after a ?> closing tag. That's why many php pages don't use the closing tag.
Second: You should always create and return some meaningful message. In other words and error message in many cases should not be much different than your regular webpages except that it should contain some information for the user telling what went wrong and maybe how to fix the problem.
The error may be temporary so you have to mention that. If it is permanent then of course can say that, too. If you look around the web you will see and find some creative solutions for error pages. That's all up to you and outside my jurisdiction.