Objective
I want all URLs of missing pages to forward to my 404 page which is in my root as 404error.aspx
Problem
So far only the URLs that have a .aspx
will work. For example, if you enter 4error.aspx
you will be redirected to the error page.
/404error.aspx?aspxerrorpath=/4error.aspx
Background
I am not a .NET developer, so I just took over this project which uses classic .aspx
. So I am not using any Microsoft product to build any of this in templates or frameworks. I am just coding in Sublime text.
What I already researched
- 404 Redirecting for non aspx pages - the answers there were incomplete or do not work.
- 404 does not append aspxerrorpath for non aspx pages - but that was a bit different than what I want.
I view many articles that I found on Google but nothing had a full example that actually worked.
Code
This is the entire code that I started with in my web.config
web.config
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/404error.aspx" />
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-US"
uiCulture="de-DE"
/>
</system.web>
</configuration>
I have also found this example from Microsoft (404error.aspx was my modification)
http://msdn.microsoft.com/en-us/library/vstudio/bb397417%28v=vs.100%29.aspx
web.config (2)
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true" />
<!-- Turn on Custom Errors -->
<customErrors mode="On"
defaultRedirect="/404error.aspx">
<error statusCode="404" redirect="/404Error.aspx"/>
</customErrors>
</system.web>
</configuration>
That also did not handle pages that did not have the .aspx
Then I tried this example from
web.config (3)
<?xml version="1.0"?>
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/404error.aspx" />
<error statusCode="404" redirect="~/404error.aspx" />
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-US"
uiCulture="de-DE"
/>
</system.web>
<system.webServer>
<httpErrors>
<remove statusCode="401" subStatusCode="-1" />
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<!-- full url when responsemode is Redirect -->
<error statusCode="401" path="http://foo.com/default.htm" responseMode="Redirect" />
<!-- local relative path when responsemode is ExecuteURL -->
<error statusCode="403" path="~/404error.aspx" responseMode="ExecuteURL" />
<error statusCode="404" path="~/404error.aspx" responseMode="ExecuteURL" />
<error statusCode="500" path="~/404error.aspx" responseMode="ExecuteURL" />
</httpErrors>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Obviously I have to change the paths for other-than-404 pages but I just wanted to test it for those errors. That last web.config
works even worse.
Can you please tell me what I need to modify. I would appreciate a full <configuration>
because I keep getting confused what goes where.
EDIT 1
I had read that many developers were suggesting to just make it a HTML page. So now my page is 404.html
Here is my updated web.config
<configuration>
<system.web>
<customErrors mode="On"
redirectMode="ResponseRewrite">
<error statusCode="404"
redirect="~/404.html"/>
</customErrors>
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-US"
uiCulture="de-DE"
/>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom"
defaultResponseMode="File">
<remove statusCode="404"/>
<error statusCode="404"
path="~/404.html"/>
</httpErrors>
</system.webServer>
</configuration>