在IIS上重写不适用于CakePHP的规则

Cam*_*ron 1 php iis cakephp

我一直在尝试使用以下web.config设置在CakePHP的IIS上运行重写规则:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
          <rules>
            <rule name="Imported Rule 1" stopProcessing="true">
              <match url="^$" ignoreCase="false" />
              <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>
            <rule name="Imported Rule 3" stopProcessing="true">
              <match url="^$" ignoreCase="false" />
              <action type="Rewrite" url="webroot/" />
            </rule>
            <rule name="Imported Rule 4" stopProcessing="true">
              <match url="(.*)" ignoreCase="false" />
              <action type="Rewrite" url="webroot/{R:1}" />
            </rule>
            <rule name="Imported Rule 5" stopProcessing="true">
              <match url="^(.*)$" ignoreCase="false" />
              <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>
Run Code Online (Sandbox Code Playgroud)

所有的CSS,JS和其他文件都可以正常工作.正如在主页上加载一样,但其他页面/pages/about只显示404!

编辑:IIS中设置的屏幕截图:

在此输入图像描述

有什么问题?谢谢

Tar*_*pta 5

IIS7本身不支持.htaccess文件.虽然有些附加组件可以添加此支持,但您也可以将htaccess规则导入IIS以使用CakePHP的本机重写.为此,请按照下列步骤操作:

1.使用Microsoft的Web平台安装程序安装URL Rewrite Module 2.0或直接下载(32位/ 64位).

2.在CakePHP根文件夹中创建一个名为web.config的新文件.

3.使用记事本或任何XML安全编辑器将以下代码复制到新的web.config文件中...

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
            <rule name="Redirect static resources" stopProcessing="true">
            <match url="^(ico|img|css|files|js)(.*)$" />
            <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="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            </conditions>
            <action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
            </rule>
            <rule name="Imported Rule 2" stopProcessing="true">
              <match url="^$" ignoreCase="false" />
              <action type="Rewrite" url="/" />
            </rule>
            <rule name="Imported Rule 3" stopProcessing="true">
              <match url="(.*)" ignoreCase="false" />
              <action type="Rewrite" url="/{R:1}" />
            </rule>
            <rule name="Imported Rule 4" stopProcessing="true">
              <match url="^(.*)$" ignoreCase="false" />
              <conditions logicalGrouping="MatchAll">

                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
              </conditions>
              <action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
            </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)