Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom 404 error page not working on IIS 8.5

I have recently moved host and have had to set up Customer Errors again in IIS.

I can go to IIS Admin and Error Pages as follows:

Custom Errors on IIS

Then I can go to the Custom Errors, and have set up the options as follows:

Customer Errors setup

That creates my web.config file as follows:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="ExecuteURL">
            <remove statusCode="500" subStatusCode="100" />
            <remove statusCode="500" subStatusCode="-1" />
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" prefixLanguageFilePath="" path="/error_404.asp" responseMode="ExecuteURL" />
            <error statusCode="500" prefixLanguageFilePath="" path="/error_500.asp" responseMode="ExecuteURL" />
            <error statusCode="500" subStatusCode="100" path="/error_500.asp" responseMode="ExecuteURL" />
        </httpErrors>
    </system.webServer>
</configuration>

When I test the pages, the 505 error works fine, and redirects to the right page, but the 404 doesn't redirect and returns the standard IIS 404 error. I have confirmed that the 404 error page is in place on the server in the correct location.

I can't see what else I need to do.

like image 827
4532066 Avatar asked Jul 05 '15 06:07

4532066


2 Answers

Got it working in the end (helped by finding this: http://forums.iis.net/t/1173965.aspx), using:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
            <remove statusCode="500" subStatusCode="100" />
            <remove statusCode="500" subStatusCode="-1" />
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" path="/error_404.asp" responseMode="ExecuteURL" />
            <error statusCode="500" prefixLanguageFilePath="" path="/error_500.asp" responseMode="ExecuteURL" />
            <error statusCode="500" subStatusCode="100" path="/error_500.asp" responseMode="ExecuteURL" />
        </httpErrors>
    </system.webServer>
</configuration>
like image 90
4532066 Avatar answered Nov 16 '22 08:11

4532066


I was having a similar problem where I have a custom 404 page at /Error/Missing but it wasn't showing up for static files that didn't exist or for folders/directories that DID exist (but shouldn't be served by MVC). The controller for Missing page has the following:

    Response.AddHeader("Content-Type","text/html; charset=utf-8");
    Response.TrySkipIisCustomErrors = true;
    Response.StatusCode = (int)HttpStatusCode.NotFound; // 404

Also I wasn't getting my custom error page if I returned the following in a controller:

return HttpNotFound();

I could change the IIS default errors to a blank page if I set PassThrough:

<httpErrors existingResponse="PassThrough" />

Changing it to "Replace" made the default IIS errors show again.

I also had a section in my web.config, but I've taken it out as I'm IIS 8.5 it doesn't look like it's needed any more.

<system.web>
    <customErrors mode="Off">
</system.web>

So basically I couldn't get rid of default IIS messages - either one-liner or the more detailed ones. My httpErrors section looked like this:

<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
    <remove statusCode="404" />
    <error statusCode="404" path="/Error/Missing" />
</httpErrors>

Finally I came across this question and I was looking at the other answer on this question and realised that I could try a ResponseMode on each error line. I thought that wouldn't be necessary as I had the defaultResponseMode set - but it makes a difference!!

So, if you want to serve up a custom 404 page use this httpErrors module:

<httpErrors errorMode="Custom">
    <remove statusCode="404" />
    <error statusCode="404" path="/Error/Missing" responseMode="ExecuteURL" />
</httpErrors>

I've put all these details here so this hopefully shows up for someone else searching the same things I did - hope it helps!

like image 7
Matt Kemp Avatar answered Nov 16 '22 08:11

Matt Kemp