yii2 - 如何在 IIS 上正确配置 web.config 以发布 YII2 Advanced

标签 yii2 iis-7 web-config yii2-advanced-app

我正在安装一个基于 YII2 高级主题的应用程序。它在 Apache 上运行良好,但在 IIS 7 上安装时,漂亮的 url 会非常糟糕,我正在使用以下 web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <configSections>
        <sectionGroup name="system.webServer">
            <sectionGroup name="rewrite">
                <section name="rewriteMaps" overrideModeDefault="Allow" />
                <section name="rules" overrideModeDefault="Allow" />
            </sectionGroup>
        </sectionGroup>
    </configSections>

    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

但是这个是行不通的,因为当打开一个特定的路由时,它会尝试去那个路由而不是在 YII2 中构建正确的路由。

这个问题在抛出的error上可以看出,可以看到它试图打开物理路径\web\user\login,这显然不存在。什么时候它应该打开像 .../index.php?r=controller/action

这样的东西

enter image description here

在 apache 上我会使用:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

任何可以帮助我解决这个问题的想法。

最佳答案

经过几天的搜索并试图理解为什么 dos 不起作用。运气不太好。

终于找到了。

首先:
确保您已经在 IIS7 上安装了 URL Rewrite。

如果您有 web.config 配置,并且它仍然路由到物理目录而不是 Controller /操作路由,则几乎 100% 意味着您缺少 URL Rewrite。是的,这是一个不一定安装的插件。

所以继续吧,从URL Rewrite下载并安装它到你的国际空间站上

enter image description here

第二:
这是实际工作的 web.config。请注意,我以:match url=".*" 结尾,还请注意我添加了一行来处理图像和其他静态内容。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <configSections>
        <sectionGroup name="system.webServer">
            <sectionGroup name="rewrite">
                <section name="rewriteMaps" overrideModeDefault="Allow" />
                <section name="rules" overrideModeDefault="Allow" />
            </sectionGroup>
        </sectionGroup>
    </configSections>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Yii2 Routing that works" stopProcessing="true">
                    <match url=".*" />
                    <conditions  logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html" negate="true" />                            
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

最后 如果您使用的是 YII2 高级主题,请确保在两个 Web 目录中更新和配置您的 web.config。

这个 menas,你需要戴上它们:
C:\webroot****\www\backend\web\web.config
C:\webroot****\www\frontend\web\web.config

并且一定要更新你的 main.php «or config.php on basic» 在高级上,您需要确保您的 enablePrtettyUrl 已针对前端和后端进行配置:

C:\webroot****\www\backend\config\main.php
C:\webroot****\www\frontend\config\main.php

'urlManager' => [
   'enablePrettyUrl' => true,
   'showScriptName' => false,
...
]

我仍在调试并尝试解决一些小问题,如果我发现其他问题,我会更新 os。

关于yii2 - 如何在 IIS 上正确配置 web.config 以发布 YII2 Advanced,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49494959/

相关文章:

php - Yii 2 如何获取当天的所有数据

mysql - Yii2 find()/QueryBuilder 中带 WHERE 条件的 SELECT 子查询

css - 如何在 Yii2 中注册小部件 Assets ?

php - YII2 : two models form validation, 第二个模型需要依赖第一个模型输入

ms-access - 允许在 IIS7 中下载 MDB

c# - 如何将公共(public)属性添加到要在 web.config 中配置的自定义 SqlMembership Provider

asp.net-mvc - ASP.NET MVC3 中的随机 session 超时

iis - "405 method not allowed"在 IIS7.5 中为 "PUT"方法

.net-3.5 - IIS7.0 到 IIS6.0

c# - web.config 批处理 ="false"