0

I'm running a PHP based application on IIS8, under Windows Server 2012. I'm trying to use a custom error page for failed authentication attempts - by replacing the 401 error page.

Here is my web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpRedirect enabled="false" destination="https://my.website.co.uk" />
        <rewrite>
            <rules>
                <rule name="Hide Yii Index" stopProcessing="true">
                    <match url="." />
                    <action type="Rewrite" url="index.php" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                </rule>
            </rules>
        </rewrite>
<httpErrors errorMode="Custom" existingResponse="Replace">
       <remove statusCode="401" subStatusCode="-1" />
       <error statusCode="401" path="/errors/401.htm" responseMode="ExecuteURL" />
</httpErrors>
    </system.webServer>
</configuration>

When I attempt to look at the website without supplying credentials, all I see is a blank page. The source of the page is totally blank too - no single html tag.

Any ideas how I make this work?

1 Answer 1

0

Think about what you are doing here. You are saying if the user has not been authenticated correctly, display this other page to the user. But this (401) page can only be displayed to authenticated users. So IIS is not allowed to show that page to the user and displays a blank page instead.

There are at least two ways around this, change your httpErrors to:

<httpErrors errorMode="Custom" existingResponse="Replace">
   <remove statusCode="401" subStatusCode="-1" />
   <error statusCode="401" path="errors\401.htm" responseMode="File" />
</httpErrors>

By changing the responseMode to File, iis is not executing a URL anymore and instead loads the content of a file and sends it to the user. This always works regardless of the authenication status. The path is relative to the root of the web site.

The other option is to add a web.config to your errors folders and allow anonymous authentication for this directory, it only has error pages after all.

6
  • Thanks - that makes sense. Unfortunately I tried that and now it's just serving the default 401 page again. Also tried enabling Anonymous authentication on the errors folder, but that just gives the empty page again. Any more ideas?
    – Hippyjim
    Jun 16, 2015 at 6:58
  • @Hippyjim - Not sure what's going on. I tested the solution with responseMode="File" and it worked fine for me. Jun 16, 2015 at 7:29
  • Me neither - thanks anyway. Just wish I could get some kind of clue or error report, but IIS likes keeping secrets
    – Hippyjim
    Jun 16, 2015 at 7:49
  • Enable failed request tracing, that may give you some input on what's going on. Jun 16, 2015 at 8:34
  • Thanks, unfortunately that's just taken me further into the rabbit hole. I can't enable failed request tracing as it looks like I don't have that installed (!!?). I can't afford to mess things up while the server's in heavy use and installing extra software seems a bit risky - can't afford for windows to want to restart after the install. Have to do this late at night or something.
    – Hippyjim
    Jun 16, 2015 at 9:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .