简体   繁体   English

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

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

I am trying to move a complex redirect logic implemented in Global.asax into a set of rules for the IIS URL Rewrite module in a classic ASP.NET website. 我试图将在Global.asax中实现的复杂重定向逻辑移到经典ASP.NET网站中的IIS URL Rewrite模块的一组规则中。 Shortly, the logic must redirect requests like 不久,逻辑必须重定向请求,例如

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

to

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

(optional and variable parts are in square and curly brackets). (可选和可变部分在方括号和大括号中)。

The first 3 main things I implemented with URL Rewrite rules are the followings: 我使用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>

These rules allow to redirect requests like http://www.ourdomain.com/product-name/default.aspx to https://ourdomain.com/product-name . 这些规则允许将http://www.ourdomain.com/product-name/default.aspx请求重定向到https://ourdomain.com/product-name

However, the browser developer tools report that this conversion causes 301 redirects. 但是,浏览器开发人员工具报告说此转换导致301重定向。 I tried to find a solution of this redirect chain problem in the Internet and found this post . 我试图在Internet上找到重定向链问题的解决方案,并找到了这篇文章 After reading it, I managed to recode my rules to have just one final 301 redirect with URL rewriting: 阅读后,我设法将规则重新编码为只有最后的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>

However, this approach looks non-natural. 但是,这种方法看起来很不自然。 It makes hard adding new rules because of these workarounds with the underscore character. 由于这些解决方法带有下划线字符,因此很难添加新规则。 And maybe, such several rewrite operations for one request may impact on the website performance. 也许,对一个请求的这种数种重写操作可能会影响网站性能。

Is there another, more elegant solution, of the 301 redirect chain problem for the URL Rewrite module? URL重写模块是否还有另一个301重定向链问题的更优雅的解决方案?

This is not a limitation of IIS Url Rewrite, but the way the web works. 这不是IIS Url Rewrite的限制,而是网络的工作方式。 A redirection sends a header to the browser telling it that it should navigate to another place. 重定向会将标头发送到浏览器,告诉标头应导航到另一个位置。 If you have 3 rules that apply to the same original request, and the 3 apply a redirection, then you'll always get 3 redirections no matter how you do it: IIS Url Rewrite, Apache .htaccess or your own custom code. 如果您有3条规则适用于相同的原始请求,而3条规则适用于重定向,那么无论您如何执行,都将始终获得3条重定向:IIS Url Rewrite,Apache .htaccess或您自己的自定义代码。

The solution you've implemented is a "patch" to change several redirections into internal rewrites in the server and a final "catch" of those special rewrites to transform them into a unique redirection at the end (BTW, if you have any real page that strats with _ you won't be able to access it with this extra rule). 您实施的解决方案是将多个重定向更改为服务器内部重写的“补丁”,以及对这些特殊重写的最终“捕捉”,最后将它们转换为唯一的重定向(BTW,如果您有任何真实页面的话)与_分层,您将无法使用此额外规则访问它)。 That's not bad and won't affect your performance, but it's ugly. 不错,不会影响您的表现,但是很丑。

I'm not a SEO expert but I think the 301 chaining thing to reach a final similar URL won't hurt your SEO at all in recent times, if that's what worries you (read the final part specially). 我不是SEO专家,但是我认为301链接可以到达最终的相似URL 几乎不会对您的SEO造成任何伤害 (如果您担心的话)(请特别阅读最后一部分)。 And being 301 redirects they will affect only the first visit of your users, so probably won't hurt your loading times or conversions either anyway. 作为301重定向,它们只会影响用户的首次访问,因此无论如何都不会损害您的加载时间或转化。

However, if you want to avoid that, try to make a single rule for your purpose, doing all the transformations at once. 但是,如果要避免这种情况,请尝试为您的目的制定一条规则,一次完成所有转换。 In your case, you want to transform this: 在您的情况下,您想对此进行转换:

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

(I guess /Home/ can be present sometimes or not) (我猜/Home/有时可以出现也可以不出现)

into this: 到这个:

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

Then you can use this single rule: 然后,您可以使用以下一条规则:

<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>

and you will transform everything with a single rule (so, just one redirect). 然后您将用一个规则转换所有内容(因此,只有一个重定向)。 The regular expression in the match URL part will identify this specific kind of URL (old product URLs) and the conditions (matching any of them) will take care of the HTTPs and domain change. 匹配URL部分中的正则表达式将标识这种特定类型的URL(旧产品URL),而条件(匹配其中任何一个)将处理HTTP和域更改。 The stopProcessing="true" will keep other rules to act, and you'll have this specific kind of URLs under control. stopProcessing="true"将使其他规则继续起作用,您将可以控制这种特定类型的URL。

Please note that I've not tested this rule and maybe it has minor problems. 请注意,我尚未测试此规则,也许它存在一些小问题。 But you get the idea... 但是你明白了...

You'll need extra rules after this one to take care of other less-specific URLs, such as your general HTTP to HTTPS or canonical domain rules. 在此之后,您将需要其他规则来处理其他不太特定的URL,例如您的常规HTTP到HTTPS或规范域规则。 But in the case you want to resolve, this will achieve what you want in a single step. 但是,在您要解决的情况下,这将一步实现您想要的目标。

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

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