简体   繁体   中英

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.

Currently IIS is only handling a single redirect (at most) only if it finds a template name within the URL. eg IIS redirects a URL such as

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. The final URL above is a valid one according to my application.

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). It works with some logic to maintain PATH_INFO and QUERY_STRING intact. 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.

Current IIS redirect pattern

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

Previously our web application was supporting the following URL

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

Than we used URL ReWriting in IIS and removed index.cfm from the URL. But we had trouble that there were multiple/chained redirections to redirect from non-https, non-www, or no trailing slash, etc.

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.

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

Hope this could help others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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