【问题标题】:Apache .htaccess rewrite rule to IIS FastCGI web.config rule problemsApache .htaccess 重写规则到 IIS FastCGI web.config 规则问题
【发布时间】:2019-11-28 23:19:31
【问题描述】:

所以我有一个自定义的 PHP MVC 框架,它在 Apache 中工作得非常好,但是很难让它适应 IIS 10 FastCGI。

这是我的结构:

/website/
- htaccess / web.config
- app folder -> Libraries (Core, Controller, Database), Controllers, Models, Views, Includes, Helpers, etc
- public folder -> css, js, index.php, htaccess / web.config

根 htaccess 如下所示:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^$ public/ [L]
  RewriteRule (.*) public/$1 [L]
</IfModule>

将其翻译成这个 web.config:

<rules>
  <rule name="Imported Rule 1" stopProcessing="true">
    <match url="^$" />
    <action type="Rewrite" url="public/" />
  </rule>
  <rule name="Imported Rule 2" stopProcessing="true">
    <match url="(.*)" />
    <action type="Rewrite" url="public/{R:1}" />
  </rule>
</rules>

因此,点击路由到“网站/公共”的“网站”效果很好。

问题在于公共 web.config。这是它的 htaccess:

<IfModule mod_rewrite.c>
  Options -Multiviews
  RewriteEngine On
  RewriteBase /website/public
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>

Web.config 翻译:

<rule name="Imported Rule 1-1" stopProcessing="true">
  <match url="^(.+)$" ignoreCase="false" />
  <conditions logicalGrouping="MatchAll">
    <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?url={R:1}" appendQueryString="true" />
</rule>

例如,现在我应该可以访问“网站/工作”或“网站/工作/列表/0”。在 Apache 中工作得很好,但我在 IIS 上得到了 404。两天没解决。

请欣赏任何提示!谢谢。

[编辑] 有什么方法可以跟踪请求吗?我已经按照此链接https://wengier.com/debugging-iis-rewrite-rules/ 尝试了 IIS 中的失败请求跟踪,但 IIS 似乎没有向日志输出任何 404 错误。我的 FRT 设置似乎缺少链接中显示的“RequestRouting”和“Rewrite”选项。

[EDIT2] 两个 web.config 文件之间似乎存在冲突。如果我禁用根 web.config 文件,然后输入“website/public/jobs”(而不是“website/jobs”,它会起作用。

【问题讨论】:

    标签: apache .htaccess iis url-rewriting web-config


    【解决方案1】:

    打开 iis 选择 /website/public 打开 URL 重写导入规则 UI,注释掉重写基础 /website/public 行并导入剩余规则。这将导致重写规则保存到该目录中的 web.config 文件中。这将为您提供与 apache 上的重写基础相同的行为。

    【讨论】:

    • 是的,我不得不这样做,因为导入工具无法识别 RewriteBase 指令。
    【解决方案2】:

    我修好了。这两个 web.config 相互冲突。所以,我完全放弃了 public/web.config。

    我的根 web.config 现在看起来像这样:

    <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
            <match url="^$" />
            <action type="Rewrite" url="public/" />
        </rule>
        <rule name="Imported Rule 2" stopProcessing="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAll">
                <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="public/index.php?url={R:1}" appendQueryString="true" />
        </rule>
    </rules>
    

    【讨论】:

      猜你喜欢
      • 2012-08-22
      • 2017-02-15
      • 1970-01-01
      • 2012-12-06
      • 2012-07-12
      • 2014-10-29
      • 2014-11-21
      • 1970-01-01
      • 2014-07-17
      相关资源
      最近更新 更多