将多个 IIS7 重写规则(重定向)合二为一 [英] Combine Multiple IIS7 Rewrite Rules (Redirects) Into One
问题描述
我正在使用 iis7 的 URL Rewrite 模块来完成几件事:
I am using iis7's URL Rewrite module to accomplish several things:
- 从非 www 到 www 的 301 重定向规则
- 301 将规则 .info 重定向到 .com(移动到我域的 .com 版本)
- 301 从旧页面重定向规则,例如/page-name.asp 到/page-name
我已经能够将前两项合并为一个规则,第三项是它自己的规则.问题是在请求如下url的情况下会生成两个301重定向:
I have been able to combine the first two into one rule, and the 3rd item is it's own rule. The problem is that two 301 redirects are generated in a case of requesting a url like:
site.info/page-name.asp/
site.info/page-name.asp/
首先执行 301 以:
First a 301 is done to:
www.site.com/page-name.asp(例如添加 www 并且 .info 转到 .com)
www.site.com/page-name.asp (e.g. www is added and .info goes to .com)
然后第二个 301 是从那里完成的:
Then a second 301 is done from that to:
www.site.com/page-name
www.site.com/page-name
我的问题是:如何将这些组合起来,以便只发生一个 301 重定向而不是两个?以下是当前位于我的 web.config 中的两个规则:
My question is: how can I combine these so that only ONE 301 redirect occurs instead of two? Here are the two rules as they currently sit in my web.config:
<rule name="SEO - 301 Redirect - .info to .com AND force WWW" stopProcessing="false">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^site.info$" />
</conditions>
<action type="Redirect" url="{ToLower:http://www.site.com/{R:1}}" redirectType="Permanent" />
</rule>
<rule name=".aspVersion-to-friendlyvia301" stopProcessing="false">
<match url="(.*).asp" />
<action type="Redirect" url="{R:1}" />
</rule>
推荐答案
我似乎找到了自己问题的答案.这是一个小技巧,但完成了所有必需的 url 转换(例如删除尾部斜杠、非 www 到 www、toLowerCase、删除目录的默认文档以及任何其他必要的重定向,例如页面名称更改).
I seem to have found the answer to my own question. It is a bit of a hack but accomplishes all required url transformations (e.g. trailing slash removal, non-www to www, toLowerCase, removal of default document for directories, and any other redirects necessary such as a page name change).
我所说的问题实际上被称为301重定向链接",解决方案相当优雅,在这里:
The problem I was talking about is actually called "chaining of 301 redirects", and the solution is presented rather elegantly, here:
http://www.seomoz.org/blog/what-every-seo-should-know-about-iis#chaining
这篇关于将多个 IIS7 重写规则(重定向)合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!