locked
404 Redirect loop Issue (Web.config) RRS feed

  • Question

  • User-195907812 posted

    I have the following in my (webforms) web.config:

    <system.web>
         <customErrors mode="On" defaultRedirect="/errors/404">
              <error statusCode="500" redirect="/errors/500" />
         </customErrors>
    </system.web>

    <system.webServer>
         <httpErrors errorMode="Custom" existingResponse="Replace">
              <clear />
              <error statusCode="404" path="/errors/404" responseMode="Redirect"/>
         </httpErrors>
    </system.webServer>

    When a 404 occurs, I want to redirect the user to /errors/404 (and log the 404 in a database).

    However, I still need /errors/404 to set the 404 header, but when I do this, it's causing a loop.

    protected override void Render(HtmlTextWriter writer)
    {
         base.Render(writer);
         Response.StatusCode = 404;
    }

    protected override void OnInit(EventArgs e)
    {
         base.OnInit(e);
         Response.Buffer = true;
         Response.StatusCode = 404;
         Response.Status = "404 Not Found";
         Response.TrySkipIisCustomErrors = true;
    }

    How would I stop the loop? 

    Thanks

    Thursday, December 6, 2018 4:53 PM

All replies

  • User-893317190 posted

    Hi RageRiot,

    If you only want to  when 404 occurs ,record log,I suggest you could use the  Application_EndRequest event, it will fire on every request ends.

    Your configuration will not stop redirecting until the response's status code is not 404, which in your case will cause  a loop.

    You could register the Application_EndRequest in Global.asax.

    My code:

     protected void Application_EndRequest(object sender, EventArgs e)
            {
              HttpResponse response=     HttpContext.Current.Response;
                if (response.StatusCode == 404)
                {
                    Response.Write(HttpContext.Current.Request.Path);
                }
    
            
            }

    After I enter the path "my404".

    Best regards,

    Ackerly Xu

    Friday, December 7, 2018 2:13 AM
  • User-195907812 posted

    Unfortunately, this still loops.

    I'm trying to implement a solution where:

    - I can write the 404 URL to the database

    - I can display (redirect?) a friendly 404 error page

    - A 404 response header is sent

    Friday, December 7, 2018 10:22 AM
  • User-893317190 posted

    Hi RageRiot,

    Not sure whether this will meet your requirement.

    Below is my code in endRequest,

         protected void Application_EndRequest(object sender, EventArgs e)
            {
              HttpResponse response=     HttpContext.Current.Response;
                if (response.StatusCode == 404 && HttpContext.Current.Request.Path !="/404.aspx")
                {
                    Response.Redirect("/404.aspx");
                }
                if(HttpContext.Current.Request.Path == "/404.aspx")
                {
                    response.StatusCode = 404;
                }
    
            
            }

    This will make all the pages that are not found  redirected to 404.aspx and 404.aspx will return 404 header.

    But all the pages that are not found themselves will return a 302 status code because of redirecting.

    Best regards,

    Ackerly Xu

    Monday, December 10, 2018 1:38 AM