【问题标题】:Routing web.config to index.php within folder将 web.config 路由到文件夹内的 index.php
【发布时间】:2020-11-05 13:58:58
【问题描述】:

我没有使用 Windows Web 服务器的经验,所以我有点迷茫。我想设置一个将所有 url 路由到 index.php 的文件夹。我尝试了几种不同的配置,但其中任何一个都不起作用。

我将如何在 .htaccess 中做到这一点:

RewriteEngine on
RewriteBase /Software
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /folder/index.php?/$1 [NC,QSA,L]

我对 web.config 的尝试:

<configuration> 
  <system.webServer>
      <rewrite>
          <rules>
              <rule name="Redirect To Index" stopProcessing="true">
                  <match url=".*" />
                  <conditions>
                      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  </conditions>
                  <action type="Rewrite" url="/folder/index.php" />
              </rule>
          </rules>
      </rewrite>
  </system.webServer>
</configuration>

【问题讨论】:

    标签: windows iis web-config webserver web.config-transform


    【解决方案1】:

    将以下规则放在 Software 目录下的 webconfig 文件中。如果 webconfig 文件不存在,请创建它。 IIS可以识别其中的规则。

    <configuration> 
      <system.webServer>
    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions>
            <add input="{R:1}" pattern="^(index\.php|images|robots\.txt)" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="/folder/index.php?/{R:1}" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
      </system.webServer>
    </configuration>
    

    IIS 重写规则的设计理念不同。它不支持RewriteBase 命令。我们只需将webconfig 文件放在相应的文件夹中即可。其余 URL 规则将在特定目录中起作用。
    此外,其余的 URL 规则可以通过以下 IIS 功能自动转换为 IIS 重写规则。
    https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/importing-apache-modrewrite-rules
    关于如何转换RewriteBase命令。
    https://forums.iis.net/t/1162790.aspx
    如果有什么我可以帮忙的,请随时告诉我。

    【讨论】:

    • 感谢您的帮助。我尝试了您的配置,似乎它对路由没有影响。我会尝试更好地解释发生了什么。基本上我是文件夹“/folder/”中的webconfig文件,而不是主项目文件夹。当我尝试将您的配置放入“/folder/”时,它仍然根据主要路由规则进行路由。
    • 相对地址依赖于根网站的路径,绝对地址依赖于WebConfig所在的路径。假设文件夹目录下有一个index.php页面和一个webconfig文件。重写动作的 URL 应该是 “/folder/index.php” 或 “index.php” 将 webconfig 文件放在 Software 文件夹下表示 URL 重写规则只在 Software 文件夹中有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 2016-02-29
    • 2016-09-02
    • 2014-11-15
    • 1970-01-01
    • 2021-08-09
    相关资源
    最近更新 更多