Recently, I found out that my WordPress powered blog did not correctly handle 404 - File Not Found errors. When a request was made to a non-existent page, then instead of getting nice WordPress based error page, visitors used to get a generic IIS 7.0 404 error page. Somehow, I have missed that part of the configuration when I set up WordPress initially. The fix for that turned out to be very simple, but since it seems to be a very common configuration task when hosting WordPress on IIS 7.0, I decided to explain the necessary configuration steps.
By default IIS is configured to return detailed error messages for local requests and custom error messages for remote requests. So when request is made to http://ruslany.net/boguspage, the server returns this:
This is a custom IIS error page. Typically, this is not the kind of response you would want your blog visitors to see. Instead, it would be nice to return an error page generated by WordPress. Almost all WordPress themes have a template for the 404 error called 404.php. This template tells WordPress how to generate an error page, that should be returned when URL is not found on the server. To return that page instead of the generic IIS page do the following:
- In IIS Manager open the “Error Pages” feature UI for your web site
- Select the status code 404 from the list view and click “Edit” action
- In the “Edit Custom Error Page’ dialog choose an option “Execute a URL on this site”
- Enter the URL as “/index.php?error=404“
Here is how the IIS Manager UI will look like:
Alternatively, you can add the following configuration section into the <system.webServer> section of web.config file located at the root folder of your web site:
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/index.php?error=404" responseMode="ExecuteURL" />
Note, that by default the IIS “Error Pages” feature is not delegated for being configured via web.config file. This means that if you use shared hosting, you can add <httpErrors> section to web.config file only if hosting provider has delegated the “Error Pages” feature.
Once the configuration change has been made, any request to a non-existent URL will result in the standard WordPress error response:
Read the complete post here