【问题标题】:Web.Config Url rewrite works but Redirect not workingWeb.Config Url 重写工作但重定向不起作用
【发布时间】:2021-08-25 03:05:21
【问题描述】:

我有以下 web.config 我在其中添加了 URL 重写和重定向规则。这是部分工作。

代码:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <caching enabled="false" />
        
        <rewrite>
        
          <rules>
          
              
            <rule name="Rewrite to pages.asp">
              <match url="^pages/([_0-9a-z-]+)" />
              <action type="Rewrite" url="pages.asp?p={R:1}" />
            </rule>
            
            <rule name="Redirect from pages.asp">
                <match url="^pages/([_0-9a-z-]+)" />
                <action type="Redirect" url="pages/{R:1}" redirectType="Permanent" />
            </rule>
            
          </rules>
        </rewrite>
            
    </system.webServer>
</configuration>

网址结构如下:

https://www.example.com/pages.asp?p=my-article

重写应该是这样的(并且有效):

https://www.example.com/pages/my-article/

如果我手动访问此 URL,它可以工作,但重定向部分无法正常工作。这段代码有问题。

【问题讨论】:

  • 您是否收到诸如“此页面未正确重定向”之类的错误?如果是这样,可能是由于无限重定向循环。考虑将stopProcessing="true" 添加到重写规则中,以防止(再次)应用重定向规则。
  • 为什么需要重定向规则?请解释一下你想用它做什么。
  • @FlorianWinter 我没有收到这种错误。但是为了安全起见,我添加了这个参数。
  • @LexLi 实际上应用程序中有很多页面(由 Google 索引),因此我希望它们重定向。我已经尝试过入站/出站规则,但我无法正确理解它们。
  • 如果您的目标是将旧链接(如 pages.asp?p=my-article)重定向到无扩展版本,那么您编写的重定向规则完全错误(因为您尝试匹配错误的内容)。

标签: asp.net web-config iis-10


【解决方案1】:

url重写规则的执行顺序是从上到下,执行完Rewrite规则后,重写后的URL与你的重定向规则中的参数不匹配,这就是重定向不起作用的原因。

更详细的错误信息,可以使用失败请求跟踪查看。

Using Failed Request Tracing to Trace Rewrite Rules

【讨论】:

  • 我认为意图是“.asp” URL 应该重定向到“RESTfull” URL,而“RESTfull” URL 应该重写为“.asp” URL。重写规则与“.asp” URL 不匹配,所以我认为这不是重定向不起作用的原因。但也许我错过了什么......
【解决方案2】:

规则都写错了,通过使用 IIS 重写规则管理器,我现在有一个工作代码。这可能会帮助其他有同样问题的人。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <caching enabled="false" />
        
        <rewrite>
            <rules>
                
                <rule name="RedirectPages" stopProcessing="true">
                    <match url="^pages\.asp$" />
                    <conditions>
                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                        <add input="{QUERY_STRING}" pattern="^p=([^=&amp;]+)$" />
                    </conditions>
                    <action type="Redirect" url="pages/{C:1}" appendQueryString="false" />
                </rule>
                <rule name="RewritePages" stopProcessing="true">
                    <match url="^pages/([^/]+)/?$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="pages.asp?p={R:1}" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="OutboundPages" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^(.*/)pages\.asp\?p=([^=&amp;]+)$" />
                    <action type="Rewrite" value="{R:1}pages/{R:2}/" />
                </rule>
                
            </outboundRules>
          
          
        </rewrite>
            
    </system.webServer>
</configuration>

【讨论】:

    猜你喜欢
    • 2013-01-27
    • 2013-02-27
    • 2015-01-29
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    相关资源
    最近更新 更多