将Yii2 .htaccess文件转换为web.config

时间:2019-01-31 12:13:53

标签: azure .htaccess iis yii2 web-config

我正在将yii2应用程序部署到Azure,因此必须创建等效于.htaccess的web.config。我尝试按照此论坛和google上可用的答案进行操作,甚至尝试自动生成文件,但仍然收到错误。这是我的两个.htacess文件

Options -Indexes

<IfModule mod_rewrite.c> 
  RewriteEngine on

  RewriteCond %{REQUEST_URI} !^public
  RewriteRule ^(.*)$ frontend/web/$1 [L] 
</IfModule>

# Deny accessing below extensions
<Files ~ "(.json|.lock|.git)">
Order allow,deny
Deny from all
</Files>

# Deny accessing dot files
RewriteRule (^\.|/\.) - [F]

第二个.htaccess文件:

RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

以下第一个无法正常工作的.htaccess文件给出了我要用作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.web>
      <customErrors mode="Off"/>
    </system.web>

    <system.webServer>
        <security>
          <requestFiltering>
            <fileExtensions>
              <add fileExtension=".json" allowed="false" />
              <add fileExtension=".git" allowed="false" />
              <add fileExtension=".lock" allowed="false" />
            </fileExtensions>
          </requestFiltering>
        </security>
        <rewrite>
          <rules>
            <rule name="Imported Rule 1" stopProcessing="true">
              <match url="^(.*)$" ignoreCase="false" />
              <conditions>
                <add input="{REQUEST_URI}" pattern="^public" ignoreCase="false" />
              </conditions>
              <action type="Rewrite" url="frontend/web/{R:1}" />
            </rule>
          </rules>
        </rewrite>
        <httpErrors errorMode="Detailed"></httpErrors>
    </system.webServer>
</configuration>

任何可以帮助我的人都指出了我的代码存在的问题。谢谢

编辑:

我已将错误报告添加到web.config文件中,现在出现此错误:

The 'type' attribute must be specified on the 'section' tag. (D:\home\site\wwwroot\web.config line 6)

The 'type' attribute must be specified on the 'section' tag. (D:\home\site\wwwroot\web.config line 7)

我不确定这是什么意思,因为大多数答案都没有aatribute类型。

编辑2:

尝试了很多重写web.config文件之后;现在,我陷入了一个错误,该错误涉及之前已定义的“ system.webServer”,我无法识别该错误的实际位置:

这是错误跟踪

Server Error in '/' Application.
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

Parser Error Message: Section or group name 'system.webServer' is already defined. Updates to this may only occur at the configuration level where it is defined.

Source Error: 


An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

Source File: D:\home\site\wwwroot\web.config    Line: 4 


Show Additional Configuration Errors:


The 'type' attribute must be specified on the 'section' tag. (D:\home\site\wwwroot\web.config line 6)

The 'type' attribute must be specified on the 'section' tag. (D:\home\site\wwwroot\web.config line 7)


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3163.0

0 个答案:

没有答案