Web.Config IIS 重写规则

Web.Config IIS Rewrite Rules

我有一个站点可以将所有请求重定向到 index.php。但是我需要对我的 web.config.

稍作改动

这是重定向部分中的当前内容:

<rewrite>
    <rules>
        <rule name="Process" 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="index.php" />
        </rule>
    </rules>
</rewrite>

但是,我需要它,这样如果有人访问 /news,它就会转到 news.php 而不是 index.php

我会使用什么规则,我会把它放在哪里?我来自 Apache 背景,但我的客户使用 IIS。

您可以在现有规则之上插入另一条规则,并进行一些更改以重定向到您的位置。

这也可以在 IIS 管理器中完成。通过这样做将允许您测试您使用的语法,看看它是否适用于您想要的网址。

<rule name="Rewrite-News" patternSyntax="ExactMatch" stopProcessing="true">
    <match url="news" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="news.php" appendQueryString="false" />
</rule>

所以它看起来有点像这样:

<rewrite>
    <rules>
        <rule name="Rewrite-News" stopProcessing="true">
            <match url="news" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
            <action type="Rewrite" url="news.php" appendQueryString="false" />
        </rule>
        <rule name="Process" 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="index.php" />
        </rule>
    </rules>
</rewrite>