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

时间:2023-03-19
本文介绍了重写规则不适用于 IIS 上的 CakePHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用根文件夹中的以下 web.config 设置为 CakePHP 的 IIS 获取重写规则:

I have been trying to get rewrite rules to work on IIS for CakePHP using the following web.config settings which is in the root folder:

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

All the CSS, JS and other files work fine. As does loading in the home page, but other pages such as /pages/about just show a 404!

在 IIS 中设置的屏幕截图:

Screenshot of setup in IIS:

有什么问题吗?谢谢

推荐答案

我对 IIS 没有太多经验,但是,回到这个问题,我注意到一些明显的问题.IIS 已经导入了 CakePHP 的所有三个 .htaccess,而不管它们包含在哪个目录中.

I don't have much experience with IIS but, returning to this question, I'm noticing some glaring issues. IIS has imported all the three of CakePHP's .htaccess without any regard as to which directory they are contained in.

CakePHP 附带了两个额外的 .htaccess 文件,以便用户可以轻松地将安装放入 Apache 中,并且无论他们尝试使用哪个 URL,他们都应该始终(希望)被重定向到正确 .htaccess文件和东西应该正常工作"(TM):

CakePHP comes with two additional .htaccess files so that users can easily throw an installation into Apache and, regardless of the URL they try, they should always (hopefully) be redirected to the correct .htaccess file and things should "just work" (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.

The way IIS has imported these files, assuming your document root is set to /, rules 3-4 (from the second .htaccess file) are not needed. But more importantly, due to the catch-all regex (.*) in rule 2 (from the first .htaccess file), no rule beyond rule 2 will ever execute - meaning requests will never be passed to index.php.

无论如何,您没有使用 Apache,因此您不能随意地将 CakePHP 安装放在周围,期望它们能够正常工作.在任何 Web 服务器(Apache、IIS、Nginx 等)上的生产环境(为了性能和安全)中使用的正确文档根目录是恰当命名的 /app/webroot 目录.该目录包含 index.php、静态文件和正确的 .htaccess 文件.

Anyway, you are not using Apache, so you can't haphazardly throw CakePHP installations around expecting them to just work. The correct document root to use in production environments (for performance and security) on any web server (Apache, IIS, Nginx, etc) is the aptly named /app/webroot directory. This directory contains index.php, static files, and the correct .htaccess file.

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

After that, all you should need is rule 5 (from the correct .htaccess file). It basically reads: "match all requests and send them to index.php, unless there is a real file or directory we can serve":

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

这篇关于重写规则不适用于 IIS 上的 CakePHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:SaveMany 用于在 cakePHP 中同时更新多个记录不起作 下一篇:带有“hasAndBelongsToMany"控制器的 CakePHP 条件查

相关文章

最新文章