The plain old and common 404 error can get a little old and for that reason I am going to fill you in on how to handle bad status reports that you can easily handle. In order to follow along you are going to need access to your .htaccess file along with a normal way to handle URLs without special mapping.
The Code
In order to make this work successfully we are first going to need to open up the .htaccess file. If you do not currently have one but know that your host supports the functionality then you can just open up notepad and save the file as “.htaccess”. With the file in place we are going to need to add the following lines of code to make the server check for proper results.
1 2 | #force error document handling. ErrorDocument 404 /error/404.php |
As you can see in the above code we have a comment followed by an error document code. We have set the status type that we are after to 404 and then pulled the page from the directory “error” and requested the file named 404.php.
When requesting a page like this you are not actually redirecting the user to the new page but rather you are bringing in the 404.php to be displayed. This allows the user to keep the attempted url in the window in case they need to make a change to the spelling.
Adding More
If you would like to handle things such as 401 requests as well you can do the same thing on the next line below. It will allow you to handle a different error with the same page or you can always change up the page to display something else. This allows for unique page error messages.
1 2 3 | ErrorDocument 401 /error/404.php ErrorDocument 404 /error/404.php ErrorDocument 500 /error/500.php |
Why It Won’t Work With URL Mapping
When working with URL mapping in a way that allows you to build the application from the index file you shouldn’t have to deal with handling status’ at all. This is because you handle all of the fake directories and if a directory doesn’t match up to a predefined one then you can simply just throw it to a 404 page if you desire.