I need to create custom 404 handling in Umbraco, and I need to handle all requests including static files. The reason for this post is that I've searched the forum and stack overflow without any satisfying result. Alot of questions and few answers. There seems to be four possible ways to do this.
Setting error404 in umbracoSettings.config
I won't go into this one.
Writing your own INotFoundHandler
I have got this to work. And with the following web.config settings it can handle all static requests also.
As I understand this is the correct way do this now. Writing the IContentFinder is pretty easy, there is for example a guide for doing that here: http://creativewebspecialist.co.uk/2013/08/07/the-new-way-to-do-a-404-umbraco-handler/ I can't however get handling static requests to work. Is it even possible? There are some parameters that seem to affect this which are:
customErrors in web.config
httpErrors in web.config
trySkipIisCustomErrors in umbracoSettings.config
Each of these have different possible settings and trying to figure out the correct one feels like pulling a bunch of levers at random and finding the correct combination.
Writing your own HttpHandler
If IContentFinder can't handle static requests this might be a possibility. However then the question is which HttpContext-event to hook up to and if/when during the request life cycle UmbracoContext.Current is available.
After some researching on this it seems that UmbracoContext is always null for not found static file requests. ApplicationContext is available though so as a workaround you could do UmbracoContext.EnsureContext() even though I've understood it's not recommended.
Probably overkill for just 404 handling but have you looked at SEOChecker? Not only does it handle static files also, but it logs the 404's also for you and let your end users fix a broken link. then the next time it can be a 301 redirect instead of 404.
Ok if I understand you, you want to know what the path is /arbitraryPathThatDoesNotExistAndThatWillRespondWithA404 instead of your 404 node?
The reason for this is that using this not existing path, Umbraco's internal routing is now working and when it doesn't find this node it will load the 404 page from the config.
With the IContentFinder, I was able to search for an error page based on a DocType alias, allowing editors to move/edit/recreate the 404 page without having to worry about the URI. Since the httpErrors elements require a static path to be set, we lose the flexibility of the IContentFinder.
I'm thinking the node id in umbracoSettings.config is unnecessary if I use a legitimate path for the 404 in web.config. I foresee the path being more static than the node id when deploying to different environments, plus, I don't want to have to create separate transforms of the umbracoSettings.config.
Handling 404 for static files in Umbraco 7
Hi,
I need to create custom 404 handling in Umbraco, and I need to handle all requests including static files. The reason for this post is that I've searched the forum and stack overflow without any satisfying result. Alot of questions and few answers. There seems to be four possible ways to do this.
Setting error404 in umbracoSettings.config
I won't go into this one.
Writing your own INotFoundHandler
I have got this to work. And with the following web.config settings it can handle all static requests also.
However the INotFoundHandler is being removed in Umbraco v8.0 which makes this solution deprecated. http://issues.umbraco.org/issue/U4-5415
Writing your own IContentFinder
As I understand this is the correct way do this now. Writing the IContentFinder is pretty easy, there is for example a guide for doing that here: http://creativewebspecialist.co.uk/2013/08/07/the-new-way-to-do-a-404-umbraco-handler/ I can't however get handling static requests to work. Is it even possible? There are some parameters that seem to affect this which are:
Each of these have different possible settings and trying to figure out the correct one feels like pulling a bunch of levers at random and finding the correct combination.
Writing your own HttpHandler
If IContentFinder can't handle static requests this might be a possibility. However then the question is which HttpContext-event to hook up to and if/when during the request life cycle UmbracoContext.Current is available.
After some researching on this it seems that UmbracoContext is always null for not found static file requests. ApplicationContext is available though so as a workaround you could do UmbracoContext.EnsureContext() even though I've understood it's not recommended.
Hi,
Probably overkill for just 404 handling but have you looked at SEOChecker? Not only does it handle static files also, but it logs the 404's also for you and let your end users fix a broken link. then the next time it can be a 301 redirect instead of 404.
Hope this helps,
Richard
Over two years later and I'm having trouble finding a modern, best practices solution for this.
Anyone have some input?
Hi Matthew,
what we always do for showing the custom
404
page in Umbraco for static files is this:You can read more about this on the following community thread: https://our.umbraco.org/forum/ourumb-dev-forum/bugs/50332-Custom-404-Only-Works-for-Extensionless-and-ASPX-URLs
Hope this helps!
/Michaël
Thanks Michaël,
Seems like this removes the need for a custom IContentFinder, so long as I don't mind using a static path as my 404 page. Would you agree?
Hi Matthew,
yes no need for a custom
IContentFinder
for this approach. The second part of your comment I don't understand completely, can you be more specific?/Michaël
Ok if I understand you, you want to know what the path is
/arbitraryPathThatDoesNotExistAndThatWillRespondWithA404
instead of your 404 node?The reason for this is that using this not existing path, Umbraco's internal routing is now working and when it doesn't find this node it will load the 404 page from the config.
Hope this helps.
/Michaël
With the IContentFinder, I was able to search for an error page based on a DocType alias, allowing editors to move/edit/recreate the 404 page without having to worry about the URI. Since the httpErrors elements require a static path to be set, we lose the flexibility of the IContentFinder.
Yes correct but you can set the id of the 404 page in the umbracoSettings.config.
Then you can still change the 404 in the backoffice as long as the id stays the same ( which will normally be ).
/Michaël
Great, that's what I want to hear.
I'm thinking the node id in umbracoSettings.config is unnecessary if I use a legitimate path for the 404 in web.config. I foresee the path being more static than the node id when deploying to different environments, plus, I don't want to have to create separate transforms of the umbracoSettings.config.
Thanks for your help!
No problem, glad I could help you!
Have a nice day.
/Michaël
is working on a reply...