Search code examples
cakephpmod-rewritecakephp-2.0

Moved CakePHP to Azure App Service, rewrites aren't working


Copied my CakePHP v2.2 app from a linux server to Azure App Service for dev/testing. I get that .htaccess won't work, so I've added a web.config in each folder \ & \app\ & \app\webroot\. The root of the site works, the home page loads, reads data form SQL, the CSS and JS loads. CSS files are hosted in \app\webroot\css, but accessed via example.com\css\file.css, so I assume some sort of rewrite is working.

The issue is when going to a page, i.e. example.com/about, I get the error The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

I've seen most if not all posts on here about hosting this on IIS, and again I think my web.configs are there. What am I missing?

web.config in \

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="rule1A" stopProcessing="true">
                    <match url="^$"  />
                    <action type="Rewrite" url="app/webroot/"  />
                </rule>
                <rule name="rule2A" stopProcessing="true">
                    <match url="(.*)" ignoreCase="false" />
                    <action type="Rewrite" url="app/webroot/{R:1}"  />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

web.config in \app\

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="rule 1A" stopProcessing="true">
                    <match url="^$"  />
                    <action type="Rewrite" url="webroot/"  />
                </rule>
                <rule name="rule 2A" stopProcessing="true">
                    <match url="(.*)"  />
                    <action type="Rewrite" url="webroot/{R:1}"  />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

web.config in \app\webroot\

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="CakePHP" stopProcessing="true">
                  <match url="^(.*)$" ignoreCase="true" />
                  <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                  </conditions>
                  <action type="Rewrite" url="index.php" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Solution

  • Fixed it! Found this article again, and updated just the web.config at \ and removed the web.configs from the other two locations. Works like a charm!

    https://book.cakephp.org/2.0/en/installation/url-rewriting.html#url-rewrites-on-iis7-windows-hosts

    web.config in \

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Rewrite requests to test.php"
                      stopProcessing="true">
                        <match url="^test.php(.*)$" ignoreCase="false" />
                        <action type="Rewrite" url="app/webroot/test.php{R:1}" />
                    </rule>
                    <rule name="Exclude direct access to app/webroot/*"
                      stopProcessing="true">
                        <match url="^app/webroot/(.*)$" ignoreCase="false" />
                        <action type="None" />
                    </rule>
                    <rule name="Rewrite routed access to assets(img, css, files, js, favicon)"
                      stopProcessing="true">
                        <match url="^(img|css|files|js|favicon.ico)(.*)$" />
                        <action type="Rewrite" url="app/webroot/{R:1}{R:2}"
                          appendQueryString="false" />
                    </rule>
                    <rule name="Rewrite requested file/folder to index.php"
                      stopProcessing="true">
                        <match url="^(.*)$" ignoreCase="false" />
                        <action type="Rewrite" url="index.php"
                          appendQueryString="true" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>