customErrors, ResponseRewrite, aspxerrorpath

I had the pleasure today to be dealing with another pain in the arse issue with  I do love .net, I will not become a PHP programmer as I feel it is more robust, in most areas!  But just sometimes you wish Microsoft would think before they add a feature!

<customerrors mode="On" defaultredirect="/error.html">
    <error statuscode="404" redirect="/errors/404.html" />
    <error statuscode="500" redirect="/error.html" />

We’ve all used this. Most .net websites won’t be running without it these days. Also these days, many of us use URL rewriting to change .aspx into .html. I know I know, it doesn’t make a blind bit of difference to search engines. I personally, and others may agree, it just looks nicer to have .html over .aspx (and .php, .asp, etc).

So you setup all your rewrite rules, everything is working correctly. You have Xenu’d your site, no pages are broken, but suddenly you are getting errors coming through, reports that pages are broken, and Google is indexing your 404 pages! WTF? Well this happened to me today.

We have a client with a really old site, where all the pages ran off of “thispage.aspx” and it was the querystrings that changed what was being displayed. What a shit way to do things especially in this day and age, that’s probably why they called in my company to fix it! Problem was because Google indexes so many sites, and although we have cleaned things up, it still found somewhere in the multiverse a site/link/something pointing to the old page. So tried to index it again.

But it’s all good, our site didn’t break, the user got a 404 error page “/errors/404.html?aspxerrorpath=thispage.aspx” which is nice but .net websites have this problem in that it redirected the user to “/errors/404.html?aspxerrorpath=thispage.aspx” and instead of getting a 404 http status, the site popped out a 302. And Google indexed the page 🙁 We had a 404 error page indexed by Google. Pants! But Webmaster tools let us delete it. Great! Now how to stop it happening again?

The Solution

<customerrors mode="On" defaultredirect="/error.html" redirectmode="ResponseRewrite">
     <error statuscode="404" redirect="~/errors/404.html" />
     <error statuscode="500" redirect="/error.html" />

I was told by numerous websites that by adding redirectMode=”RepsonseRewrite” to the customErrors config section in your web.config it should all work. Baws it did. It certainly didn’t redirect, which was the main aim. And it certainly gave the 404 http status code. But I also received a white screen of death! More reading… I was then told to add a Reponse.something or other but I can’t remember what it is now. It didn’t work. Baws again!

Thankfully if you request a /youaintfindingthispage.html then my website’s URL rewriting handled this error and pumped out the correct status codes. But why was I getting a white screen of death, instead of the file which I pointed IIS to? “What if the RepsonseRewrite had to then be rehandled?” I thought. So I quickly threw in a rule into the rewrite section of my web.config :

<rule name="Errors" stopprocessing="true">
    <match url="^(.*)\.(aspx|php|asp)" />
    <conditions logicalgrouping="MatchAll" trackallcaptures="false">
        <add input="{REQUEST_FILENAME}" matchtype="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchtype="IsDirectory" negate="true" />
    <action type="Rewrite" url="/virtual.aspx?p={R:1}&u={R:2}" />

This was placed at the end of all the rules so that if a URL didn’t match anything previous, it could be checked by this last rule. BING BING BING! It worked, thankfully as I couldn’t take it any more. I didn’t have the nasty redirect and I didn’t have the nasty white screen of death!

2 thoughts on “customErrors, ResponseRewrite, aspxerrorpath”

  1. An impressive share! I have just forwarded this onto a coworker who had
    been doing a little research on this. And he actually bought me dinner simply
    because I found it for him… lol. So allow me to
    reword this…. Thanks for the meal!! But
    yeah, thanx for spending time to talk about this issue
    here on your blog.

    My site: the sims 3 cd key

Leave a Reply

Your email address will not be published.