Web.config – Custom error pages not working

I have tried the following (and also tried with the commented-out uncommented instead) but only get an error:

The resource you are looking for has been removed, had its name
changed, or is temporarily unavailable.

In the web.config of a published website project:

  <system.webServer>
    <httpErrors errorMode="Custom"  existingResponse="Replace">
      <remove statusCode="404"/>
      <!--<error statusCode="404" responseMode="File" path="Error404.htm"/>-->
      <error statusCode="404" responseMode="ExecuteURL" path="http://example.com/Error/404.htm"/>
    </httpErrors>
  </system.webServer>

I try it by changing the url in the browser from .../default.aspx (which is fine) to .../abc.aspx.

Is this the correct way to redirect to error pages, or is there some mistake here?

EDIT

I’ve found that if I try http://example.com/nonExistingPage – it does redirect to the error page. But not fromhttp://example.com/Folder/nonExistingPage

EDIT 2

The problem was partially solved by specifying the path after example.com. However – the site is published to example.com/subfolder and when someone navigates to example.com/nonExistingFolder – the custom error page is not shown.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try this in web.config (includes 500 error support as well):

<configuration>
    ...
    <system.web>
        ...
        <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/500.htm">
            <error statusCode="404" redirect="~/Error/404.htm" />
            <error statusCode="500" redirect="~/Error/500.htm" />
        </customErrors>
        ...
    </system.web>
    ...
    <system.webServer>
        ...
        <httpErrors errorMode="Custom">
            <remove statusCode="404" />
            <error statusCode="404" path="/Error/404.htm" responseMode="ExecuteURL" prefixLanguageFilePath="" />
            <remove statusCode="500" />
            <error statusCode="500" path="/Error/500.htm" responseMode="ExecuteURL" prefixLanguageFilePath="" />
        </httpErrors>
        ...
     </system.webServer>
     ...
</configuration>

I would also recommend using .aspx pages rather than .htm so that you can ensure the proper status code is set in the response headers.
<%@ Page Language="C#" %>

<% Response.StatusCode = 404; %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>404 Not Found</title>
</head>
<body>
    404 Error
</body>
</html>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x