1

我可以看到出站 URL 重写工作,因为它用新 URL 替换了页面上的所有 URL。所以无论我把这个放在哪里:

/post.asp?topic=question&id=123

它将其重写为...

/question/123

但是,当我访问重写的 URL 时,/question/123我收到 404 错误。看起来重写部分可以工作,但是一旦你转到重写的 URL,加载 URL 怎么样?

我在 IIS > URL 重写中使用了“创建友好 URL”向导并检查了出站和重定向规则。就像这个例子解释的那样:http ://learn.iis.net/page.aspx/497/user-friendly -url---规则模板/

这是 Web.Config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <remove name="RewriteUserFriendlyURL1" />
                <remove name="RedirectUserFriendlyURL1" />
                <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
                    <match url="^post\.asp$" />
                    <conditions>
                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                        <add input="{QUERY_STRING}" pattern="^topic=([^=&amp;]+)&amp;id=([^=&amp;]+)$" />
                    </conditions>
                    <action type="Redirect" url="{C:1}/{C:2}" appendQueryString="true" redirectType="Permanent" />
                </rule>
                <rule name="RewriteUserFriendlyURL1" 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="/post.asp?topic={R:1}&amp;id={R:2}" />
                </rule>
            </rules>
            <outboundRules>
                <remove name="OutboundRewriteUserFriendlyURL1" />
                <rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^(.*/)/post\.asp\?topic=([^=&amp;]+)&amp;(?:amp;)?id=([^=&amp;]+)&amp;(?:amp;)$" />
                    <action type="Rewrite" value="{R:1}{R:2}/{R:3}/" />
                </rule>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>
4

1 回答 1

1

您是否修改了模式?我使用向导创建了具有相同步骤的规则。但是我的配置文件与你的不同,它运行顺利。比较模式,尤其是OutboundRewriteUserFriendlyURL1

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
                    <match url="^post\.asp$" />
                    <conditions>
                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                        <add input="{QUERY_STRING}" pattern="^topic=([^=&amp;]+)&amp;id=([^=&amp;]+)$" />
                    </conditions>
                    <action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
                </rule>
                <rule name="RewriteUserFriendlyURL1" 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="post.asp?topic={R:1}&amp;id={R:2}" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^(.*/)post\.asp\?topic=([^=&amp;]+)&amp;(?:amp;)?id=([^=&amp;]+)$" />
                    <action type="Rewrite" value="{R:1}{R:2}/{R:3}/" />
                </rule>
                <preConditions>
                    <preCondition name="ResponseIsHtml1">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>
于 2012-05-27T15:14:57.640 回答