Posted 27th of March 2012 by Kern Herskind Nightingale

As I recently migrated the “Apropos Sitecore” blog from Typepad to Sitecore I put some effort in to setting up legacy URL rewriting. In case I missed anything I wanted to ensure that the 404 handling on the new blog was set up appropriately.

I have always felt that the default way Sitecore handles 404 request was a bit annoying. If a users misspells a URL or perhaps misses out a couple of characters when copying a URL they will be redirected to the /sitecore/service/notfound.aspx URL with a cryptic query string - not very helpful. I suspect that today’s web savvy users find it pretty frustrating (I do!) having to retype the entire URL rather than just correcting what was wrong.

Luckily both IIS and Sitecore has support for rewriting error URLs rather than redirecting to a 404 page. Basically this means that the URL stays the same in the browser but on the server the request is transferred to another page - in this case a 404 error page.

There is a document on SDN with details of how to configure 404. There is a short chapter called “Consistent HTTP 404 Page Not Found Management” on how to configure a custom 404 page for Sitecore and IIS. Unfortunately it does not say anything about using rewrites rather than redirects.

After a bit of nosing around I found that Sitecore has a configuration option called RequestErrors.UseServerSideRedirect. Setting this to true will make Sitecore do URL rewrites rather than redirects. 

Configuration example for IIS7

First configure Sitecore to do rewrites by creating a file called CustomErrorRedirect.config in the /App_Config/Include folder with the following snippet:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <settings>
            <setting name="ItemNotFoundUrl">
                <patch:attribute name="value">/path-to/404-file.aspx</patch:attribute>
            </setting>
            <setting name="LinkItemNotFoundUrl">
                <patch:attribute name="value">/path-to/404-file.aspx</patch:attribute>
            </setting>
            <setting name="RequestErrors.UseServerSideRedirect">
                <patch:attribute name="value">true</patch:attribute>
            </setting>
        </settings>
    </sitecore>
</configuration>

Then add this to section in the Web.config to have IIS follow the same behaviour (handles page not found for non-ASP.Net requests - eg. the .html extension):

<httpErrors errorMode="Custom">
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" path="/path-to/404-file.aspx" responseMode="ExecuteURL" />
</httpErrors>

Also remember to have your “404-file.aspx” page set the Response.StatusCode = 404; and make it look pretty!



blog comments powered by Disqus

About the author

Kern is a Sitecore Specialist with more than 10 years experince achitecting and developing Sitecore solutions. Kern was awarded Sitecore MVP in 2009, 2011 and again in 2015.
Currently Kern is helping the Sitecore Product team in shaping the future of the platform.

Kern Herskind Nightingale