【问题标题】:IIS web.config rewrite if no default document exists如果不存在默认文档,则 IIS web.config 重写
【发布时间】:2020-07-04 16:05:18
【问题描述】:

我有一个用于 IIS 的web.config,如果没有定义的页面(即index.php),它可以部分地将请求重写到另一个本地wwwroot 文件夹。

所以像:www.blah.net/something/index.php 这样的 url 显示的页面是正确的。 但是,www.blah.net/something/ 会重写到另一个 URL 并忽略该目录中的 index.php

<configuration>
<system.webServer>
<defaultDocument enabled="true" />

<rewrite>
            <rules>
                <rule name="RewriteAll" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="_h5ai/public/index.php" appendQueryString="true" />
                </rule>
            </rules>
</rewrite>

        <caching enabled="false" enableKernelCache="true" />
</system.webServer>
</configuration>

【问题讨论】:

  • 你有什么问题?你是说这就是它的作用还是你想要它做的?
  • 我的默认文档被忽略了。如果站点有 index.php,除非我在其中定义带有 index.php 的 URL,否则它不会显示该站点...我希望 web.config 在返回到重写规则之前首先检查默认页面。跨度>

标签: php iis web-config


【解决方案1】:

比较简单,就是加个条件来检查文件是否存在。

<conditions>
<add input="{Document_Root}/{REQUEST_URI}/index.php" matchType="IsFile" negate="true" />
</conditions>

此外,请在清除本地浏览器缓存后或在浏览器的隐身窗口中验证结果。
如果问题仍然存在,请随时告诉我。

【讨论】:

  • 是的!我相信做到了...下面的当前web.config 似乎工作正常。如果目录中存在默认文档,它将使用它(只要它被定义为默认文档),如果不存在,它会退回到另一个位置的重写规则。
  • &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;configuration&gt; &lt;system.webServer&gt; &lt;rewrite&gt; &lt;rules&gt; &lt;rule name="RewriteAll" stopProcessing="true"&gt; &lt;match url=".*" /&gt; &lt;conditions&gt; &lt;add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /&gt; &lt;add input="{Document_Root}/{REQUEST_URI}/index.php" matchType="IsFile" negate="true" /&gt; &lt;/conditions&gt; &lt;action type="Rewrite" url="_h5ai/public/index.php" appendQueryString="true" /&gt; &lt;/rule&gt; &lt;/rules&gt; &lt;/rewrite&gt; &lt;caching enabled="false" enableKernelCache="true" /&gt; &lt;/system.webServer&gt; &lt;/configuration&gt;
【解决方案2】:

我认为您需要告诉 IIS 您的默认文档是什么。换行

<defaultDocument enabled="true" />

<defaultDocument>
  <files>
    <clear />
    <add value="index.php" />
    <add value="index.htm" />
    <add value="default.php" />
    <add value="default.htm" />        
  </files>
</defaultDocument>

IIS 将按照列出的顺序检查默认文档部分中列出的所有文件名,您可以拥有任意数量的文件名。 &lt;clear /&gt; 行告诉 IIS 忽略标准服务器设置。 (如果您不指定任何默认文件,我认为它会查找“Default.htm”、“Default.aspx”和“Default.asp”。)

【讨论】:

  • 谢谢,IIS 仍然忽略index.php 的存在。将此添加到 web.config 文件并重新启动 IIS 服务器。 https://blah.net/site/dir/index.php 有效(因为你声明了index.php),但是如果你使用:https://blah.net/site/dir/ 它被忽略并回退到_h5ai/public/index.php 行......
  • 我还发现您可以为 定义一个正则表达式模式,这可能进一步要求如果它没有找到以 .php,.htm 结尾的文件,它将执行 。来源:link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 2013-08-19
  • 2017-02-15
相关资源
最近更新 更多