繁体   English   中英

将多个301重定向合并为一个ASP.NET URL重写模块

[英]Combine several 301 redirects into one for ASP.NET URL Rewrite module

我试图将在Global.asax中实现的复杂重定向逻辑移到经典ASP.NET网站中的IIS URL Rewrite模块的一组规则中。 不久,逻辑必须重定向请求,例如

{http|https}://[www.]ourdomain.com/[Home/]Products/old-product-name/page.aspx

https://ourdomain.com/new-product-name/

(可选和可变部分在方括号和大括号中)。

我使用URL重写规则实现的前3项主要内容如下:

<rule name="Redirect to HTTPS">
    <match url=".*" />
    <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Canonical Host Name">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^ourdomain\.com$" />
    </conditions>
    <action type="Redirect" url="https://ourdomain.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Default Page">
    <match url="(.*)default.aspx" />
    <conditions>
        <add input="{REQUEST_URI}" negate="true" pattern="-default.aspx$" />
    </conditions>
    <action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>

这些规则允许将http://www.ourdomain.com/product-name/default.aspx请求重定向到https://ourdomain.com/product-name

但是,浏览器开发人员工具报告说此转换导致301重定向。 我试图在Internet上找到重定向链问题的解决方案,并找到了这篇文章 阅读后,我设法将规则重新编码为只有最后的301重定向和URL重写:

<rule name="Redirect to HTTPS">
    <match url=".*" />
    <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    </conditions>
    <action type="Rewrite" url="_{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Canonical Host Name">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^ourdomain\.com$" />
    </conditions>
    <action type="Rewrite" url="_{R:1}" redirectType="Permanent" />
</rule>
<rule name="Default Page">
    <match url="(.*)default.aspx" />
    <conditions>
        <add input="{REQUEST_URI}" negate="true" pattern="-default.aspx$" />
    </conditions>
    <action type="Rewrite" url="_{R:1}" redirectType="Permanent" />
</rule>

<rule name="FINAL REDIRECT" stopProcessing="true">
    <match url="^(_+)(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
    </conditions>
    <action type="Redirect" url="https://ourdomain.com{R:2}" />
</rule>

但是,这种方法看起来很不自然。 由于这些解决方法带有下划线字符,因此很难添加新规则。 也许,对一个请求的这种数种重写操作可能会影响网站性能。

URL重写模块是否还有另一个301重定向链问题的更优雅的解决方案?

这不是IIS Url Rewrite的限制,而是网络的工作方式。 重定向会将标头发送到浏览器,告诉标头应导航到另一个位置。 如果您有3条规则适用于相同的原始请求,而3条规则适用于重定向,那么无论您如何执行,都将始终获得3条重定向:IIS Url Rewrite,Apache .htaccess或您自己的自定义代码。

您实施的解决方案是将多个重定向更改为服务器内部重写的“补丁”,以及对这些特殊重写的最终“捕捉”,最后将它们转换为唯一的重定向(BTW,如果您有任何真实页面的话)与_分层,您将无法使用此额外规则访问它)。 不错,不会影响您的表现,但是很丑。

我不是SEO专家,但是我认为301链接可以到达最终的相似URL 几乎不会对您的SEO造成任何伤害 (如果您担心的话)(请特别阅读最后一部分)。 作为301重定向,它们只会影响用户的首次访问,因此无论如何都不会损害您的加载时间或转化。

但是,如果要避免这种情况,请尝试为您的目的制定一条规则,一次完成所有转换。 在您的情况下,您想对此进行转换:

{http|https}://[www.]ourdomain.com/[Home/]Products/old-product-name/page.aspx

(我猜/Home/有时可以出现也可以不出现)

到这个:

https://ourdomain.com/new-product-name/

然后,您可以使用以下一条规则:

<rule name="New URLs!!" stopProcessing="true">
    <match url="^(Home/){0,1}Products/(.+?/).+.aspx" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        <add input="{HTTP_HOST}" negate="true" pattern="^ourdomain\.com$" />
    </conditions>
    <action type="Redirect" url="https://ourdomain.com/{R:2}" redirectType="Permanent" />
</rule>

然后您将用一个规则转换所有内容(因此,只有一个重定向)。 匹配URL部分中的正则表达式将标识这种特定类型的URL(旧产品URL),而条件(匹配其中任何一个)将处理HTTP和域更改。 stopProcessing="true"将使其他规则继续起作用,您将可以控制这种特定类型的URL。

请注意,我尚未测试此规则,也许它存在一些小问题。 但是你明白了...

在此之后,您将需要其他规则来处理其他不太特定的URL,例如您的常规HTTP到HTTPS或规范域规则。 但是,在您要解决的情况下,这将一步实现您想要的目标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM