简体   繁体   中英

CakePHP 3.0 IIS web.config

All of my urls for images and css or being rendered with app prepended

/app/favicon.ico
/app/img/logo.png
/app/css/styles.css

The content of the pages is rendering fine, just not the images and css.

My web.config looks like this, but doesn't seem to be helping these urls. Thus they are all resulting in 404's.

<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="Imported Rule 4" stopProcessing="true">
        <match url="^(.*)$" ignoreCase="false" />
        <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
        <action type="Rewrite" url="app/index.php?url={R:1}" appendQueryString="true" />
    </rule>
    <rule name="Exclude direct access to 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="Imported Rule 1" stopProcessing="true">
        <match url="^$" ignoreCase="false" />
        <conditions logicalGrouping="MatchAll">
            <add input="{URL}" pattern="^app/webroot/" ignoreCase="false" negate="true" />
        </conditions>
        <action type="Rewrite" url="app/webroot/" />
    </rule>
    <rule name="Imported Rule 2" stopProcessing="true">
        <match url="^(.*)$" ignoreCase="false" />
        <action type="Rewrite" url="app/webroot/{R:1}" />
    </rule>
</rules>

The resulting urls should be

/favicon.ico
/img/logo.png
/css/styles.css

My directory tree on iis is:

www

app

webroot

@ADmad helped me figure this out. It turns out that this had nothing to do with web.config in IIS. It isn't a CakePHP 3.0 specific thing either. According to CakePHP IRC the CakePHP 3.x and CakePHP 2.x routing is exactly the same. This is simply IIS PHP weirdness.

The solution is to update the App.base in your config/app.php

'App' => [
    'namespace' => 'App',
    'encoding' => 'UTF-8',
    'base' => '', // default is false
    'dir' => 'src',
    'webroot' => 'webroot',
    'wwwRoot' => WWW_ROOT,
    //'baseUrl' => '/',
    'fullBaseUrl' => false,
    'imageBaseUrl' => 'img/',
    'cssBaseUrl' => 'css/',
    'jsBaseUrl' => 'js/',
    'paths' => [
        'plugins' => [ROOT . DS . 'plugins' . DS],
        'templates' => [APP . 'Template' . DS],
        'locales' => [APP . 'Locale' . DS],
    ],
],

@ADmad said the reason was, "because of some iis wackiness cake isnt able to properly detect base itself :)"

Create a web.config file on root folder and paste below lines :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Exclude direct access to webroot/*"
                  stopProcessing="true">
                    <match url="^webroot/(.*)$" ignoreCase="false" />
                    <action type="None" />
                </rule>
                <rule name="Rewrite routed access to assets(img, css, files, js, favicon)"
                  stopProcessing="true">
                    <match url="^(font|img|css|files|js|favicon.ico)(.*)$" />
                    <action type="Rewrite" url="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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM