0

I'd like to log the URLs that are requested but returning 404.

I've a php website served by IIS7. I've this in the web.config file to redirect urls that don't exist to the 404 page:

 <httpErrors>
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="https://www.example.com/404.php" responseMode="Redirect" />
</httpErrors>

I've also:

<system.web>
    <customErrors defaultRedirect="https://www.example.com/404.php" mode="On">
        <error redirect="https://www.example.com/404.php" statusCode="404" />
        </customErrors>
</system.web>

Everything I can find online says I can get the original URL requested from the php variable $_SERVER['REQUEST_URI'], but for me that always gives me 404.php.

Any idea how to get the original URL requested? The way my web.config is setup appears to stop the information being passed. If I could pass the original url as a get or post variable to the 404 page I'd be able to log it that way if that's possible?

Phil Teare
  • 417
  • 1
  • 6
  • 14
  • 1
    Possible duplicate of [Accessing original URL in IIS7 404 redirect page](https://stackoverflow.com/questions/5070695/accessing-original-url-in-iis7-404-redirect-page) – AmmoPT Aug 20 '18 at 17:16

1 Answers1

0

Thanks to AmmoPT, that link was helpful.

Firstly setup web.config correctly:

<system.webServer>
    <!-- other system.webServer stuff -->
    <httpErrors errorMode="Custom">
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="/404.php" responseMode="ExecuteURL" />
    </httpErrors>
</system.webServer>
<system.web>
    <!-- other system.web stuff -->
    <customErrors defaultRedirect="/404.php" mode="On" redirectMode="ResponseRewrite">
        <error redirect="/404.php" statusCode="404" />
    </customErrors>
</system.web>

Note the links had to be relative to make it work for me. The above stops it actually redirecting - e.g. example.com/idontexist.php will show as the url but the contents of that page will be the 404.php page

Then in the 404.php file I can detect the url with $_SERVER['REQUEST_URI'])

Phil Teare
  • 417
  • 1
  • 6
  • 14