简体   繁体   English

减少链接的URL重定向[IIS / Regex / ReWrite]

[英]Reduce Chained URL Redirects [IIS/Regex/ReWrite]

I have implemented some URL Redirects combined on IIS/ColdFusion is working fine but I am not able to deal with some regex pattern to reduce unneeded redirects. 我已经实现了一些在IIS / ColdFusion上组合的URL重定向工作正常但我无法处理一些正则表达式模式以减少不需要的重定向。

Currently IIS is only handling a single redirect (at most) only if it finds a template name within the URL. 目前,IIS仅在URL中找到模板名称时才处理单个重定向(最多)。 eg IIS redirects a URL such as 例如,IIS重定向URL,例如

http://example.com/index.cfm/something/pretty/?page=1

to

http://example.com/something/pretty/?page=1

For instance, it just removes the template name from the URL leaving everything intact following it. 例如,它只是从URL中删除模板名称,使其后面的所有内容保持不变。 The final URL above is a valid one according to my application. 根据我的申请,上面的最终URL是有效的。

But in addition, if a trailing slash (/) is not found in the final URL, ColduFusion application is handling this case and appends a forward slash at the end, then redirects to a URL that ends with a forward slash (/) before a query string (if there is). 但另外,如果在最终URL中找不到尾部斜杠(/),ColduFusion应用程序正在处理这种情况并在末尾附加正斜杠,然后重定向到以正斜杠(/)结尾的URL。查询字符串(如果有)。 It works with some logic to maintain PATH_INFO and QUERY_STRING intact. 它适用于一些逻辑,以保持PATH_INFO和QUERY_STRING完整。 But that is actually causing multiple redirects in the following situation. 但实际上,在以下情况下会导致多次重定向。

[INIT] http://example.com/index.cfm/sport/badminton 

[Redirect 1] [IIS-301]  http://example.com/sport/badminton

[Redirect 2] [CF-301]  http://example.com/sport/badminton/

Now that I want to handle all of this in IIS and cover all cases in one Rule, I am not able to make (or find) a regex pattern that could do it. 既然我想在IIS中处理所有这些并覆盖一个规则中的所有情况,我就无法制作(或找到)可以执行此操作的正则表达式模式。

Current IIS redirect pattern 当前的IIS重定向模式

^index.cfm/(.*)$ 

I have tried various ones along with the most simple 我尝试了各种各样的最简单的

^index.cfm/(.*[^/])$

But it doesn't cover a URL with a QUERY_STRING. 但它不包含带有QUERY_STRING的URL。 You can take me as real naive in making regular expressions. 在制作正则表达式时,你可以把我当作真正的天真。

Update 1: I found that the proper term for the issue is "Chained" redirects and found an article at moz.com which is kind of handling the same issue I've referred above. 更新1:我发现该问题的正确术语是“链式”重定向,并在moz.com上发现了一篇文章,它处理了我上面提到的同一问题。 I guess it should work, and while I am on it changing the rules as required on my server, I thought I should update this question with something I have found for others who may have such an issue. 我想它应该可以工作,当我在我的服务器上更改规则时,我认为我应该用我找到的其他可能有这样一个问题的人更新这个问题。 I'll update this as soon as I could use this solution to fix the issue at my side. 一旦我可以使用此解决方案来解决我的问题,我会立即更新。

Sorry that I couldn't have an answer over here but as I updated above question about an article at moz.com , I have implemented that approach and successfully recovered from the Chained/Multiple redirects. 很抱歉,我无法在这里得到答案,但是当我在moz.com上更新上述关于某篇文章的问题时,我已经实现了这种方法并成功从链式/多重定向中恢复。

Previously our web application was supporting the following URL 以前我们的Web应用程序支持以下URL

https://www.example.com/index.cfm/something/pretty/

Than we used URL ReWriting in IIS and removed index.cfm from the URL. 比我们在IIS中使用URL ReWriting并从URL中删除index.cfm。 But we had trouble that there were multiple/chained redirections to redirect from non-https, non-www, or no trailing slash, etc. 但我们遇到麻烦,有多个/链式重定向从非https,非www或没有尾部斜线等重定向。

https://moz.com/blog/what-every-seo-should-know-about-iis#chaining https://moz.com/blog/what-every-seo-should-know-about-iis#chaining

After reading the article above, I've implemented following set of rules on IIS that now takes care of all the cases we had previously handled on IIS and on ColdFusion separately. 阅读上面的文章后,我在IIS上实现了以下一套规则,现在处理我们之前在IIS和ColdFusion上分别处理的所有情况。

<rules>
<!-- rewrite url to furnish with prefix(_) to better match individual parts -->
<rule name="Remove index.cfm" stopProcessing="false">
    <match url="(.*?)/?index\.cfm/(.*)$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
    </conditions>
    <action type="Rewrite" url="_{R:2}" />
</rule>
<rule name="Add Trailing Slash" stopProcessing="false">
    <match url="(.*[^/])$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Rewrite" url="_{R:1}/" />
</rule>
<rule name="ToLower Everything in URL" enabled="true" stopProcessing="false">
    <match url="(.*)" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
        <add input="{R:1}" pattern="[A-Z]" ignoreCase="false" />
    </conditions>
    <action type="Rewrite" url="_{ToLower:{R:1}}" />
</rule>
<!-- Now redirect the final prefix-furnished URL  -->       
<!-- match if there is at least one (_) at the start of the furnished URL. Redirect to the final URL -->    
<rule name="http[non www] to https[www] redirect" stopProcessing="true">
    <match url="^(_*)(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^www\.yoursite\.com$" negate="true" />
        <add input="{HTTP_METHOD}" pattern="GET" />
        <add input="{SERVER_PORT}" pattern="80" />  
    </conditions>
    <action type="Redirect" url="https://www.example.org/{R:2}" />
</rule>
<rule name="http[www] to https[www] redirect" stopProcessing="true">
    <match url="^(_*)(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
        <add input="{SERVER_PORT}" pattern="80" />  
    </conditions>
    <action type="Redirect" url="https://www.example.org/{R:2}" />
</rule>
<rule name="https[non www] to https[www] redirect" stopProcessing="true">
    <match url="^(_*)(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^www\.yoursite\.com$" negate="true" />
        <add input="{HTTP_METHOD}" pattern="GET" />
        <add input="{SERVER_PORT}" pattern="443" /> 
    </conditions>
    <action type="Redirect" url="https://www.example.org/{R:2}" />
</rule>
<!-- this rule is supposed to run final redirect if non above redirect rules occured -->
<rule name="http// redirect" enabled="true" stopProcessing="true">
    <match url="^(_+)(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
    </conditions>
    <action type="Redirect" url="{R:2}" />
</rule>

<!-- now after failing/running all rules above when the IIS reaches at this point, it's the fully validated/funrished URL that qualifies to serve with response. Rewrite the URL to run index.cfm as a template -->
<rule name="URL ReWrite" enabled="true" stopProcessing="true">
    <match url="^(.*)$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" pattern="/admin" negate="true" />
    </conditions>
    <action type="Rewrite" url="index.cfm/{R:1}" />
</rule>

These rules are strictly according to our requirement where we wanted all requests to be routed to [https], you must check the moz.com article above for reference. 这些规则严格按照我们的要求,我们希望将所有请求路由到[https],您必须查看上面的moz.com文章以供参考。

Hope this could help others. 希望这可以帮助别人。

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

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