Well, the gatekeeper does not allow us to edit web.config file so we have to find out the way to handle the custom errors of our own Sitecore MVC sites on a shared Sitecore instance. All we can do is to play around with patching config files.
Assumptions (for a specific site)
- if a non-existent page is requested, the content of 404 page will be rendered
- if an unauthorized page is requested, the content of 404 page will be rendered as well
- if 404 page is NOT available (for some reasons: it’s deleted accidentally, misconfiguration, wrong Sitecore access right, etc), a friendly 404 static file will be rendered
- if there is an error rather than 401/404, a friendly static file will be rendered
- DO NOT handle failed media requests
- DO NOT handle requests for improper extensions
Over and over, the Sitecore community is so helpful:
- Yet Another <httpRequestBegin> Pipeline Processor to Handle ‘Page Not Found’ (404 Status Code) in Sitecore
- Sitecore 404 without 302
- https://stackoverflow.com/questions/37129899/500-custom-error-pages
For a lazy developer like me, a Sitecore package should be a good option as always.
How to install it
- Use SIM to setup a new Sitecore 8 instance. For a better practice:
- add 02 more host names
- webformssite.local
- mvcsite.local
- download this Sitecore package (sample sites) and install it to your Sitecore instance via Installation Wizard
- hopefully, you can see something like this
- add 02 more host names
- Download the module as a Sitecore package on Sitecore Marketplace or on Gitlab then install it to your Sitecore instance via Installation Wizard
Note: the module above goes with ASP.NET MVC 5.2 so we are NOT able to play it with Sitecore 8.0 (must recompile it with ASP.NET MVC 5.1), check out Sitecore compatibility table
How to use it
In fact, we don’t want to disturb all the sites (especially the ones are NOT managed by us) so if you access http://mvcsite.local/non-existent-page, you will see this one:
How is an unauthorized one?
- access http://mvcsite.local/intro, you would see this one
- log into Sitecore, access /sitecore/content/MvcSite/Home/Intro
- open the menu Security
- click Require Login in Presets section
- click OK button to apply security preset Require Login
- access http://mvcsite.local/intro, you would see this one
Enable to handle custom errors of a specific site
- open \App_Config\Include\MvcSite\MvcSiteDefinition.config
- add 02 new site properties:
- enableHandleCustomErrors=”true”
- notFoundItem=”/404″
- access http://mvcsite.local/non-existent-page, you would see this one
- access http://mvcsite.local/intro, you would see this one
Why do we should have a fallback one?
One day, our beloved editor renames 404 page item to page-not-found or whatever inside Sitecore and publishes it. He is NOT aware of 404 page item is a configured one as well.
Therefore, the end users should have a good chance to see those ones on our sites.
We have a static page http://mvcsite.local/MvcSite-404.htm and we can use it as a fallback which should be helpful for this case by adding a new site property:
- open \App_Config\Include\MvcSite\MvcSiteDefinition.config
- add a new site property:
- fallbackNotFoundItem=”/MvcSite-404.htm”
Access http://mvcsite.local/non-existent-page and http://mvcsite.local/intro, you would see those ones:
About 500 custom error page
Open \Views\Shared\HomeContent.cshtml and add @{ throw new Exception(); } to it
Access http://mvcsite.local/, you would see this one
We have another static page http://mvcsite.local/MvcSite-Error.htm and we can use it as a 500 error page by simply adding a new site property:
- open \App_Config\Include\MvcSite\MvcSiteDefinition.config
- add a new site property:
- customErrorFile=”/MvcSite-Error.htm”
- access http://mvcsite.local/, you would see this one
Notes
- We can play with Sitecore webforms site as well for 404 error by using those ones below:
- \App_Config\Include\WebformsSite\WebformsSiteDefinition.config
- \WebformsSite-404.htm
- We should put the content in cache for a better performance
Not Good Enough? Please feel free to download the source code and add your own enhancements if necessary.
Got issues?
Please send your issues (with screenshots if possible) to viet.hoang.sitecore@gmail.com so that I have a chance to understand your problem and be able to suggest the solution.
Happy Sitecore Coding!
2 thoughts on “How to handle errors of a specific site on a shared Sitecore instance”