CakePHP 3.0 IIS web.config

CakePHP 3.0 IIS web.config

我的所有图片网址和 css 或使用应用程序呈现

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

页面内容渲染良好,只是图像和 css。

我的 web.config 看起来像这样,但似乎没有帮助这些网址。因此它们都会导致 404。

<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>

生成的 url 应该是

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

我在 iis 上的目录树是:

www

应用程序

webroot

@ADmad 帮我解决了这个问题。事实证明,这与 IIS 中的 web.config 无关。它也不是 CakePHP 3.0 特定的东西。根据 CakePHP IRC,CakePHP 3.x 和 CakePHP 2.x 路由完全相同。这简直就是 IIS PHP 的怪癖。

解决方案是更新 config/app 中的 App.base。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 说的原因是,"because of some iis wackiness cake isnt able to properly detect base itself :)"

在根文件夹中创建一个 web.config 文件并粘贴以下行:

<?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>