Reputation: 27643
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="\Error\404.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.
Upvotes: 7
Views: 23998
Reputation: 10738
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>
Upvotes: 18