我已经在 IIS/ColdFusion 上实现了一些 URL 重定向组合工作正常,但我无法处理一些正则表达式模式来减少不需要的重定向。
当前 IIS 仅在 URL 中找到模板名称时才处理单个重定向(最多)。例如IIS 重定向 URL,例如
http://example.com/index.cfm/something/pretty/?page=1
到
http://example.com/something/pretty/?page=1
例如,它只是从 URL 中删除模板名称,并保留其后的所有内容。根据我的应用程序,上面的最终 URL 是有效的。
但此外,如果在最终 URL 中找不到尾随斜杠 (/),则 ColduFusion 应用程序会处理这种情况并在末尾附加一个正斜杠,然后重定向到以正斜杠 (/) 结尾的 URL查询字符串(如果有)。它与一些逻辑一起工作以保持 PATH_INFO 和 QUERY_STRING 完好无损。但这实际上会在以下情况下导致多次重定向。
[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/
现在我想在 IIS 中处理所有这些并在一个规则中涵盖所有情况,我无法制作(或找到)一个可以做到的正则表达式模式。
当前的 IIS 重定向模式
^index.cfm/(.*)$
我已经尝试了各种最简单的
^index.cfm/(.*[^/])$
但它不包括带有 QUERY_STRING 的 URL。你可以认为我在制作正则表达式时真的很天真。
更新 1:我发现该问题的正确术语是“链式”重定向,并在 moz.com 上找到了一篇文章。这是一种处理我上面提到的相同问题的方法。我想它应该可以工作,虽然我正在根据我的服务器的要求更改规则,但我想我应该用我为可能有此类问题的其他人找到的一些东西来更新这个问题。我会在可以使用此解决方案解决我身边的问题后立即更新。
最佳答案
很抱歉,我无法在这里得到答案,但是当我更新了上面关于 moz.com 上的一篇文章的问题时,我已经实现了该方法并成功地从链接/多重重定向中恢复。
以前我们的 Web 应用程序支持以下 URL
https://www.example.com/index.cfm/something/pretty/
比我们在 IIS 中使用 URL ReWriting 并从 URL 中删除 index.cfm。但是我们遇到了从非 https、非 www 或没有尾部斜杠等重定向的多个/链式重定向的问题。
https://moz.com/blog/what-every-seo-should-know-about-iis#chaining
阅读上面的文章后,我在 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>
这些规则严格按照我们的要求,我们希望所有请求都路由到 [https],您必须查看上面的 moz.com 文章以供引用。
希望这可以帮助其他人。
关于regex - 减少链接的 URL 重定向 [IIS/Regex/ReWrite],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38643510/