Custom Page 404 Using IIS 7 URL Rewriter

I almost did serious correspondence and redesign / restructuring of an existing website that receives a lot of traffic. I use the IIS 7 URL rewrite module to use URLs without extension and SEO, and also redirect requests to old pages to corresponding pages in the new site structure. Everything is going well, but I'm learning how to set up a 404 user page, and I lost consciousness. So far, I have installed the following code in the node of the web.config file:

<httpErrors errorMode="Custom" existingResponse="Replace"> <remove statusCode="404" subStatusCode="-1" /> <error statusCode="404" prefixLanguageFilePath="" path="/404.htm" responseMode="ExecuteURL" /> </httpErrors> 

The fact is that it WORKS, but not quite! If I go to a nonexistent URL, I get my 404 page, but the request gets a 200 OK response code instead of 404. I looked for various httpErrors node attributes, but no luck. Does anyone know how to show a custom 404 page And return the actual 404 status code?

Thanks!

+6
source share
2 answers

For our 404 custom handler to work, we had to do the following:

  <customErrors defaultRedirect="ErrorPage.aspx" mode="On"> <error statusCode="500" redirect="ErrorPage.aspx" /> <error statusCode="404" redirect="/404.aspx" /> <error statusCode="403" redirect="ErrorPage.aspx" /> </customErrors> 

and

  <httpErrors errorMode="Custom"> <remove statusCode="404" subStatusCode="-1" /> <error statusCode="404" prefixLanguageFilePath="" path="/404.aspx" responseMode="ExecuteURL" /> </httpErrors> 

I really think I ran into the same problem where I indicated only one type of error. It may not work without expansion, but hopefully it is on the right track.

+3
source

This worked for me, in the code behind 404.aspx:

  protected void Page_Load(object sender, EventArgs e) { this.Response.StatusCode = 404; this.Response.StatusDescription = "Not Found"; this.Response.TrySkipIisCustomErrors = true; } 
+1
source

Source: https://habr.com/ru/post/887016/


All Articles