IIS Custom Errors – Note to Self
I jut got caught out by this one so thought I best post it for posterity.
In IIS when configuring a custom error page for say a 404 error you have the option of specifying a URL or a File (via an absolute path) to the friendly error page you wish to display.
As obvious as it seems now but if you specify a URL say /errors/404.htm then you will not get a 404 header returned by the webserver instead you get a 200 ok – which in hind sight makes total sense. To get a correct 404 header returned you need to use the file option and browse to the file you wish to display.
No related posts.



You can also point to a dynamically served page and control the HTTP status code returned programmatically. This allows you to serve a dynamic page to really customize the 404 message, but still correctly return a 404 status code.
Where this is useful, is you can build a customized 404 page that attempts to either figure out the page the user was trying to request–such as redirecting an old URL to the new URL using a 301 redirect or just showing a list of pages the user may be looking for.
If you’re using ColdFusion, you can use the following code to force a 404 status to be returned to the browser:
I’ve built a standardized 404 template I can re-use on sites that allows me to configure a XML file that uses Apache-style redirect statements to help point users to the correct page they may be looking for. This is very useful if you do a massive site restructuring where URLs change.
Just some food for thought…
I use the 404 error pages to serve Search Engine Friendly URLs :
I check and parse the querystring and do a
Server.Transfer
for the links that I want to use.
If the link is something that I don’t want, I do:
response.status = “404 Not Found” to set the proper 404 header.
@Dan: Any chance of sharing your standardized 404 template approach? We will be doing a new site design and restructure soon that will cause our urls to change. I was wondering the best way to redirect to the new urls and it seems that you have an elegant solution.
Thanks!
Marcus Cox
mlcox1 at gmail dot com
You can roll your own URL mapping way, and then simply check the “cgi.query_string” in your 404 handler page. The old page URL will be in there. Then you will match the old URL to the new one and do something simple like:
Your custom 404 page content goes here
I guess I should have escaped my posted code
) Let’s try again…
You can roll your own URL mapping way, and then simply check the “cgi.query_string” in your 404 handler page. The old page URL will be in there. Then you will match the old URL to the new one and do something simple like:
<cfset variables.cNewPageUrl = “” /><!— do the matching here and populate this variable —>
<cfif len(variables.cNewPageUrl)>
<cfheader statuscode=”301″ statustext=”Moved permanently” />
<cfheader name=”location” value=”http://[yourwebsite]/#variables.cNewPageUrl#” />
<cfelse>
<cfheader statuscode=”404″ statustext=”The page cannot be found” />
<html>
<body>
Your custom 404 page content goes here
</body>
</html>
</cfif>
I tried posting some code here but it doesn’t work, not even escaping it helps.