Search code examples
phpiisweb-config

IIS web.config rewrite if no default document exists


I have a web.config for IIS that partially works to rewrite requests to another local wwwroot folder if there are no defined pages (i.e. index.php).

So a url like: www.blah.net/something/index.php displays a page correct. however, www.blah.net/something/ rewrites to another URL and ignores the index.php in that directory.

<configuration>
<system.webServer>
<defaultDocument enabled="true" />

<rewrite>
            <rules>
                <rule name="RewriteAll" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="_h5ai/public/index.php" appendQueryString="true" />
                </rule>
            </rules>
</rewrite>

        <caching enabled="false" enableKernelCache="true" />
</system.webServer>
</configuration>


Solution

  • Rather simple, just add a condition to check if the file exists.

    <conditions>
    <add input="{Document_Root}/{REQUEST_URI}/index.php" matchType="IsFile" negate="true" />
    </conditions>
    

    Besides, verify the result after clearing the local browser cache, or in an incognito window of the browser.
    Feel free to let me know if the problem persists.