Copied to clipboard

Flag this post as spam?

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


  • David Armitage 510 posts 2081 karma points
    Sep 07, 2022 @ 06:13
    David Armitage
    0

    error404 not working with media items or pdf extension

    Hi All,

    I noticed that the custom errors dont work from media items. Are maybe this is regated to certain extensions.

    For example. I have custom errors set for error404 in the UmbracoSettings.config. If I enter a wrong URL then this redirects to my 404 page ok.

    But them when I enter a bugus URL with a .pdf on the end then the redirect doesn't work and I am still presented with the generic IIS 404 - File or directory not found error screen which I am trying to avoid for a vulnerability scan.

    Any idea how to get the error404 config working for everything including PDFs?

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Sep 08, 2022 @ 10:39
    Huw Reddick
    0

    I added the check below in my startup.cs in the section where I add some response headers, that should catch any 404 (/error404 is my umbraco page)

            app.Use(async (context, next) =>
            {
                context.Response.Headers.Add("X-Xss-Protection", "1; mode=block");
                context.Response.Headers.Add("Strict-Transport-Security", "max-age=31536000");
                context.Response.Headers.Add("X-Content-Type-Options", "nosniff");
                context.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
                await next();
                if (context.Response.StatusCode == 404)
                {
                    context.Request.Path = "/error404";
                    await next();
                }
            });
    
  • David Armitage 510 posts 2081 karma points
    Sep 08, 2022 @ 10:45
    David Armitage
    0

    Hi Huw,

    Sorry I forgot to mention this was for Umbraco 8. Anyway I figure this out.

    I could not for the liufe of me figure out a solution using the standard Umbraco config. It just didn't handle certain extensions like .pdf and a few others.

    Instead I create some IIS rules to handle the errors. This seemed to work.

    <httpErrors errorMode="Custom" existingResponse="Replace">
        <remove statusCode="404" />
        <error statusCode="404" responseMode="ExecuteURL" path="/page-not-found/" />
    </httpErrors>
    
  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Sep 08, 2022 @ 10:48
    Huw Reddick
    0

    yes that's what you need to do, basically because they are considered to be static files so the error comes from IIS not Umbraco

Please Sign in or register to post replies

Write your reply to:

Draft