Web.Config IIS重写规则

时间:2016-02-17 00:27:13

标签: asp.net iis web-config

我有一个网站将所有请求重定向到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.php而不是index.php

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

1 个答案:

答案 0 :(得分:0)

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

这也可以在IIS管理器中完成。通过此操作将允许您测试您使用的语法,以查看它是否适用于您想要的URL。

<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>
相关问题