当前位置:首页 > 服务器技术 > IIS

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

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

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

所有CSS,JS和其他文件都可以正常工作.就像在首页中加载一样,但其他页面(例如/ pages / about)仅显示404!

编辑:在IIS中安装的屏幕截图:

有什么问题?谢谢

解决方法:

我在IIS方面没有太多经验,但是回到这个问题,我注意到了一些明显的问题. IIS已导入CakePHP的所有三个.htaccess文件,而无需考虑它们包含在哪个目录中.

CakePHP附带了两个附加的.htaccess文件,以便用户可以轻松地将安装扔到Apache中,无论他们尝试使用哪个URL,都应始终(希望)将其重定向到正确的.htaccess文件,并且事情应该“正常进行”(TM ):

Document root     Additional .htaccess file     Correct .htaccess file
/ --------------> /.htaccess -----------------> /app/webroot/.htaccess
/app/ ----------> /app/.htaccess -------------> /app/webroot/.htaccess
/app/webroot ---------------------------------> /app/webroot/.htaccess

假设您的文档根目录设置为/,IIS导入这些文件的方式就不需要规则3-4(来自第二个.htaccess文件).但更重要的是,由于规则2中的所有正则表达式(.*)(来自第一个.htaccess文件),规则2以外的任何规则都不会执行-意味着请求将永远不会传递到index.php.

无论如何,您都没有使用Apache,所以不能随意地将CakePHP安装扔掉,以期望它们能正常工作.在任何Web服务器(Apache,IIS,Nginx等)上的生产环境(性能和安全性)中使用的正确文档根目录是/ app / webroot目录.此目录包含index.php,静态文件和正确的.htaccess文件.

之后,您只需要规则5(来自正确的.htaccess文件).它的基本内容是:“匹配所有请求并将它们发送到index.php,除非存在我们可以提供的真实文件或目录”:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
          <rules>
            <rule name="CakePHP" 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>

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!