[英]IIS Rewrite module - how to redirect maintaining path and querystring but adding an extra querystring parameter?
我一直在撞墙,试图使一些IIS重定向规则起作用。 我已经在此处搜索并阅读了Stack Overflow和IIS.net上的内容,但这根本不起作用。
我正在本地(真实)IIS上尝试此操作,安装了重写2.0模块,并尝试在其上进行修复安装。 我在admin cmd行执行iisreset的次数比我想提的要多。
在我的主机文件中,我为URL my.test.com设置了127.0.0.1。
我要实现的目标是使用一个额外的querystring参数将子域URL重定向到主域URL,同时保留现有的path和querystring值(如果存在)。
我在网站的根文件夹中设置了以下3条规则:
<system.webServer>
<rewrite>
<rules>
<rule name="PathAndQueryString" enabled="true" stopProcessing="true">
<match url="(/\w*)(\?\w*=\w*)([&\w*=\w*]*)" />
<action type="Redirect" url="https://www.test.com{R:1}{R:2}{R:3}&param=value" appendQueryString="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
</rule>
<rule name="Querstring" enabled="true" stopProcessing="true">
<match url="(\?\w*=\w*)([&\w*=\w*]*)" />
<action type="Redirect" url="https://www.test.com{R:1}{R:2}&param=value" appendQueryString="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
</rule>
<rule name="DomainOnly" enabled="true" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://www.test.com?param=value" appendQueryString="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
不幸的是,尽管我尝试使用Edge,Chrome,Firefox和IE对所有模式进行测试并确认{R:x}捕获正确无误,但对于所有这些IIS行为,规则似乎都不存在。
测试:
以上测试均无效,无需重定向即可获得服务。
我还尝试过将条件模式用作“匹配URL”模式的一部分,但它也不起作用。 实际上,在阅读了Stack Overflow的帖子后,我将其移到了这种情况,该帖子说,当规则位于站点的根目录时,它不包含主机,但仍然无效。
任何帮助将非常感激。
更新1:对于路径和查询字符串规则,尝试从该模式的开头( / \\ w *)删除加粗部分。 没有任何效果。
更新2:尝试启用极为气质的“失败的请求跟踪”功能,并且在其实际工作时,它说匹配的URL模式均不匹配,但第三条规则已开始生效并重新定向,但并未维护该规则。 path和其他querystring参数的原因很明显。
更新3:在Edge和IE上,所有规则都不起作用。 在Chrome和FF上,仅重定向域似乎可以正常工作-但是,如果我禁用了仅域规则,然后重新启动IIS,它的行为就像规则仍然存在-FFS增强了我的实力,这种废话真的开始使我沸腾了-这不应那么该死的困难。
在给出答案之前,我先向Microsoft的Yuk Ding大喊,他在iis.net论坛上回复了此帖子的副本,并提供了大部分答案。
因此,如果您要重写规则以从1个URL重定向到另一个URL,并保留路径和查询字符串(如果存在),同时添加带有适当的&或?的硬编码URL参数,那么我们就来了。 这就是您需要的规则。
在规则中,您将从my.test.com重定向到www.test.com,但是显然您需要替换以下规则中的这两个URL,并将硬编码参数“ ExtraParam = SomeValue”更改为所需的值。
<system.webServer>
<rewrite>
<rules>
<rule name="DomainOnly" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="my.test.com" />
<add input="{REQUEST_URI}" pattern="/.+" negate="true" />
</conditions>
<action type="Redirect" url="http://www.test.com?ExtraParam=SomeValue" redirectType="Temporary" />
</rule>
<rule name="PathOnly" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/.+" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="\?.+" ignoreCase="true" negate="true" />
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
<action type="Redirect" url="http://www.test.com{C:0}?ExtraParam=SomeValue" redirectType="Temporary" />
</rule>
<rule name="Querstring" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/.+" />
<add input="{QUERY_STRING}" pattern=".*" />
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
<action type="Redirect" url="http://www.test.com{C:0}&ExtraParam=SomeValue" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
在我看来,URL Rewrite 2.0模块有一个错误。 基本的症结在于,您不能在主匹配模式中匹配任何内容,而必须对所有条件进行匹配。 因此,您可以为匹配设置模式这一事实是完全没有意义的,因为如果您尝试匹配(。*)以外的任何内容,即匹配我最初所做的任何事情,它都不会做您想要的事情,并且会导致头脑混乱。扑通砖墙。
更烦人的是,在IIS管理器中,用于此操作的UI毫无帮助。 您可以测试您的模式,而我做到了,它们可以工作,但实际上测试的只是regEx模式。
我们真正需要的是在规则级别进行测试。 换句话说,您输入一个URL,它告诉您任何规则是否匹配以及哪些条件通过或失败。 是的,如果您启用了失败的请求跟踪规则,则可以获取此信息,但是所有这些都将被启用,并且杂乱无章地工作(只需尝试清除旧日志并观察它,然后停止记录直到您将其关闭,再来一次)对于一些琐碎的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.