简体   繁体   English

IIS URL重写不使用查询字符串

[英]IIS URL Rewrite not working with query string

I thought this was pretty straightforward, but it refuses to work. 我认为这很简单,但它拒绝工作。 The old URL is 旧的URL是

http://www.site.com/?q=node/17 http://www.site.com/?q=node/17

It needs to redirect to http://www.site.com . 它需要重定向到http://www.site.com I don't need to worry about wildcards, this is the only query string parameter I need to worry about. 我不需要担心通配符,这是我唯一需要担心的查询字符串参数。 The rule I wrote looks like 我写的规则看起来像

<rule name="Node17" patternSyntax="ExactMatch" stopProcessing="true">
    <match url="http://www.site.com/?q=node/17" />
    <action type="Redirect" url="http://www.site.com" appendQueryString="False" />
</rule>

I can test the pattern inside of IIS and it matches, but when I hit the URL in a browser it doesn't redirect. 我可以测试IIS内部的模式并匹配,但是当我在浏览器中点击URL时它不会重定向。 Any thoughts? 有什么想法吗?

As described in Microsoft's documentation : Microsoft的文档中所述

It is important to understand how certain parts of the URL string can be accessed from a rewrite rule. 了解如何从重写规则访问URL字符串的某些部分非常重要。

For an HTTP URL in this form: http(s)://{host}:{port}/{path}?{querystring} 对于此格式的HTTP URL:http(s):// {host}:{port} / {path}?{querystring}

The {path} is matched against the pattern of the rule. {path}与规则的模式匹配。 The {querystring} is available in the server variable called QUERY_STRING and can be accessed by using a condition within a rule. {querystring}在名为QUERY_STRING的服务器变量中可用,可以使用规则中的条件进行访问。

Rule conditions allow defining additional logic for rule evaluation... Rule conditions are evaluated after the rule pattern match is successful. 规则条件允许为规则评估定义附加逻辑...在规则模式匹配成功后评估规则条件。

In the URL you wanted to rewrite as a redirect, your {host} = "www.site.com" , {path} = "" and {querystring} = "q=node/17" . 在您希望重写为重定向的网址中,您的{host} = "www.site.com"{path} = ""{querystring} = "q=node/17" So the {path} part in the URL you wanted to redirect is actually empty, and the rule you used in your question was matched against it and did not match. 因此,您要重定向的网址中的{path}部分实际上是空的,并且您在问题中使用的规则与之匹配且不匹配。

Your solution is indeed valid, so I'll quote it here: 你的解决方案确实有效,所以我在这里引用它:

<rule name="Node17" stopProcessing="true">
    <match url=".*" />
<conditions>
    <add input="{QUERY_STRING}" pattern="q=node/17" />
</conditions>
<action type="Redirect" url="http://www.example.com" appendQueryString="False" />
</rule>

Of course I figured it out soon after I posted. 当然,我发布后不久便想出来了。 This does it, not really sure why the exactmatch wasn't working though. 这样做,不确定为什么完全匹配不起作用。

<rule name="Node17" stopProcessing="true">
    <match url=".*" />
<conditions>
    <add input="{QUERY_STRING}" pattern="q=node/17" />
</conditions>
<action type="Redirect" url="http://www.site.com" appendQueryString="False" />
</rule>

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

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