Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • ZNS 60 posts 198 karma points
    Nov 14, 2015 @ 12:20
    ZNS
    4

    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.

    <customErrors mode="On" redirectMode="ResponseRewrite">
      <error statusCode="404" redirect="/404.aspx" />
    </customErrors>
    ...
    <httpErrors errorMode="Custom">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" prefixLanguageFilePath="" path="/404.aspx" responseMode="ExecuteURL" />
    </httpErrors>
    

    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:

    • 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.

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Nov 18, 2015 @ 15:51
    Richard Soeteman
    0

    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

  • Matthew Kirschner 323 posts 611 karma points
    Mar 20, 2018 @ 17:59
    Matthew Kirschner
    0

    Over two years later and I'm having trouble finding a modern, best practices solution for this.

    Anyone have some input?

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 21, 2018 @ 07:51
    Michaël Vanbrabandt
    1

    Hi Matthew,

    what we always do for showing the custom 404 page in Umbraco for static files is this:

    <httpErrors errorMode="Custom">
      <remove statusCode="404" />
      <error statusCode="404" path="/arbitraryPathThatDoesNotExistAndThatWillRespondWithA404" responseMode="ExecuteURL" />
    </httpErrors>
    

    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

  • Matthew Kirschner 323 posts 611 karma points
    Mar 21, 2018 @ 13:45
    Matthew Kirschner
    0

    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?

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 21, 2018 @ 14:13
    Michaël Vanbrabandt
    1

    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

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 21, 2018 @ 14:15
    Michaël Vanbrabandt
    0

    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

  • Matthew Kirschner 323 posts 611 karma points
    Mar 21, 2018 @ 14:20
    Matthew Kirschner
    0

    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.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 21, 2018 @ 14:26
    Michaël Vanbrabandt
    1

    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

  • Matthew Kirschner 323 posts 611 karma points
    Mar 21, 2018 @ 14:34
    Matthew Kirschner
    0

    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!

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 21, 2018 @ 14:35
    Michaël Vanbrabandt
    0

    No problem, glad I could help you!

    Have a nice day.

    /Michaël

Please Sign in or register to post replies

Write your reply to:

Draft