简体   繁体   English

IIS重写模块-如何重定向维护路径和查询字符串,但添加额外的查询字符串参数?

[英]IIS Rewrite module - how to redirect maintaining path and querystring but adding an extra querystring parameter?

I've been banging my head against a brick wall attempting to get some IIS redirect rules to work. 我一直在撞墙,试图使一些IIS重定向规则起作用。 I've searched and read stuff here on Stack Overflow and on IIS.net but it just doesn't work at all. 我已经在此处搜索并阅读了Stack Overflow和IIS.net上的内容,但这根本不起作用。

I'm trying it on my local (real) IIS, I have the rewrite 2.0 module installed and have tried doing a repair install on it. 我正在本地(真实)IIS上尝试此操作,安装了重写2.0模块,并尝试在其上进行修复安装。 I've done an iisreset at an admin cmd line more times than I care to mention. 我在admin cmd行执行iisreset的次数比我想提的要多。

In my hosts file I have set-up 127.0.0.1 for the URL my.test.com. 在我的主机文件中,我为URL my.test.com设置了127.0.0.1。

What I want to achieve is given a sub-domain URL redirect to the main domain URL with an extra querystring parameter whilst maintaining the existing path and querystring values if they exist. 我要实现的目标是使用一个额外的querystring参数将子域URL重定向到主域URL,同时保留现有的path和querystring值(如果存在)。

I setup 3 rules as follows that are in the root folder of the website: 我在网站的根文件夹中设置了以下3条规则:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="PathAndQueryString" enabled="true" stopProcessing="true">
                <match url="(/\w*)(\?\w*=\w*)([&amp;\w*=\w*]*)" />
                <action type="Redirect" url="https://www.test.com{R:1}{R:2}{R:3}&amp;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*)([&amp;\w*=\w*]*)" />
                <action type="Redirect" url="https://www.test.com{R:1}{R:2}&amp;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>

Unfortunately despite testing the patterns and confirming the {R:x} captures are correct when I try to test this using Edge, Chrome, Firefox and IE for all of them IIS acts like the rules don't exist. 不幸的是,尽管我尝试使用Edge,Chrome,Firefox和IE对所有模式进行测试并确认{R:x}捕获正确无误,但对于所有这些IIS行为,规则似乎都不存在。

Tests: 测试:

None of the above tests work, they get served without a redirect. 以上测试均无效,无需重定向即可获得服务。

I've also tried putting the condition pattern used as part of the Match URL pattern but it didn't work with that either; 我还尝试过将条件模式用作“匹配URL”模式的一部分,但它也不起作用。 in fact I moved it to the condition after reading a Stack Overflow post which said when rules are in the root of the site it doesn't include the host, but still nothing works. 实际上,在阅读了Stack Overflow的帖子后,我将其移到了这种情况,该帖子说,当规则位于站点的根目录时,它不包含主机,但仍然无效。

Any help would be much appreciated. 任何帮助将非常感激。

UPDATE 1: For the path and querystring rule tried removing the bolded part from that start of this pattern ( / \\w*). 更新1:对于路径和查询字符串规则,尝试从该模式的开头( / \\ w *)删除加粗部分。 Didn't have any effect. 没有任何效果。

UPDATE 2: Tried enabling the extremely temperamental "Failed Request Tracing" functionality and when it is actually working it says that none of the Match URL patterns are matching, except for the 3rd rule which has now started kicking in and redirecting but is not maintaining the path and other querystring params for obvious reasons. 更新2:尝试启用极为气质的“失败的请求跟踪”功能,并且在其实际工作时,它说匹配的URL模式均不匹配,但第三条规则已开始生效并重新定向,但并未维护该规则。 path和其他querystring参数的原因很明显。

UPDATE 3: On Edge and IE none of the rules work at all. 更新3:在Edge和IE上,所有规则都不起作用。 On Chrome and FF the domain only redirect appears to be working - however if I disable the domain only rule and restart IIS it acts as though the rule is still there - FFS give me strength this crap is really starting to boil my blood now - this should not be that damn difficult. 在Chrome和FF上,仅重定向域似乎可以正常工作-但是,如果我禁用了仅域规则,然后重新启动IIS,它的行为就像规则仍然存在-FFS增强了我的实力,这种废话真的开始使我沸腾了-这不应那么该死的困难。

Before I give the answer, a shout out to Yuk Ding at Microsoft who responded to a copy of this post on the iis.net forums and provided most of the answer. 在给出答案之前,我先向Microsoft的Yuk Ding大喊,他在iis.net论坛上回复了此帖子的副本,并提供了大部分答案。

So here we go, if you want rewrite rules that redirects from 1 URL to another maintaining the path and querystring if they exist whilst at the same time adding on a hardcoded URL parameter with the appropriate & or ? 因此,如果您要重写规则以从1个URL重定向到另一个URL,并保留路径和查询字符串(如果存在),同时添加带有适当的&或?的硬编码URL参数,那么我们就来了。 then here are the rules you will need. 这就是您需要的规则。

In the rules you are redirecting from my.test.com to www.test.com, but obviously you would need to replace those 2 URLs in the rules below as well as changed the hardcoded parameter "ExtraParam=SomeValue" to what you need. 在规则中,您将从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}&amp;ExtraParam=SomeValue" appendQueryString="false" redirectType="Temporary" />
        </rule>
    </rules>
</rewrite>

In my eyes the URL Rewrite 2.0 module has a bug. 在我看来,URL Rewrite 2.0模块有一个错误。 The basic crux is that you cannot match anything in the main match pattern you have to do everything with conditions. 基本的症结在于,您不能在主匹配模式中匹配任何内容,而必须对所有条件进行匹配。 Thus the fact that you can set a pattern for the match is completely pointless because if you try to match anything other than (.*), ie match anything like I originally did it won't do what you want and will effectively cause head-bashing-brick-wall-ness. 因此,您可以为匹配设置模式这一事实是完全没有意义的,因为如果您尝试匹配(。*)以外的任何内容,即匹配我最初所做的任何事情,它都不会做您想要的事情,并且会导致头脑混乱。扑通砖墙。

The even more annoying thing is that in IIS Manager the UI for this is very unhelpful. 更烦人的是,在IIS管理器中,用于此操作的UI毫无帮助。 You can test your patterns - and I did - and they will work, but all it's really testing is the regEx pattern. 您可以测试您的模式,而我做到了,它们可以工作,但实际上测试的只是regEx模式。

What we really need is a test at the rules level. 我们真正需要的是在规则级别进行测试。 In other words you enter a URL and it tells you whether any of the rules match and which conditions pass or fail. 换句话说,您输入一个URL,它告诉您任何规则是否匹配以及哪些条件通过或失败。 Yes you can get this information if you enabled the failing request tracing rules but it's all a long winded phaff to enable and works in a haphazard fashion (just try clearing out the old logs and watch as it then stops logged until you turn it off and on again) for something that should be trivial. 是的,如果您启用了失败的请求跟踪规则,则可以获取此信息,但是所有这些都将被启用,并且杂乱无章地工作(只需尝试清除旧日志并观察它,然后停止记录直到您将其关闭,再来一次)对于一些琐碎的事情。

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

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