After working on many multisite Sitecore configurations, it's always surprised me that Sitecore doesn't provide the ability to specify different 404, 500 & robots.txt's for each site. In the past I've implemented these pieces of functionality separately. I've now combined the three into single module which can handle all three cases. This has been released on Github and Nuget and is available for use now.
This module is hosted in Nuget and can be installed in Visual Studio via the package manager using the following command:
All you need to do then is to manually edit your Global.ascx.cs to include the reference to the error handling code.
- Get the project from Github then build project and reference the DLL in your project.
- Include the httpRequestBegin pipeline processor if you want to make use of the 404 functionality.
- Insert the required web.config sections.
- Add the error handling code into Global.ascx.cs.
After installing the module, you need to include the new attributes on your existing Sitecore sites node. If you don't want to add one of the functions to a site, then don't include that attribute and the code will fallback to the default Sitecore functionality.
404 Page usage
The 404 page functionality can be configured by populating the notFoundPageId attribute on the site node. This needs to be populated with the ID of the item you wish to use for the 404 page. If the item isn't found then it will fallback to the default Sitecore 404 functionality.
If you want to exclude certain URL's from being processed by the 404 handler then you can use the exclude404Rules configuration section in the web.config, this will allow you to exclude URL's from being processed by checking for a string value in the URL. You can check whether a URL Contains, StartsWith or EndsWith the value. The default ones are as follows:
Points to note:
This will not redirect to your 404 page as that results in a 302 response being returned to the browser (a known issue with Sitecore) which is incorrect. Instead it will simply render the 404 content on the requested URL. You then need set the response code to be 404 on that layout using the following code
500 Page usage
The 500 page functionality can be configured by populating the errorPagePath attribute on the site node. This needs to be populated with the url to forward the user to when an unhandled error is encountered. It is recommended to use a static page as the target and not a Sitecore URL, as this will then still function if major issues (e.g. database connectivity) occur.
The robots.txt functionality can be configured by populating the robotsTxtLocation attribute on the site node. This needs to be populated with the location of a text document containing the contents you wish to display when robots.txt is called for that site. If this is not specified then the standard robots.txt will be returned, if it exists.