如何识别请求是否来自URL重写模块

时间:2013-12-11 16:09:53

标签: c# iis url-rewriting arr

如果标题不清楚,请道歉,基本上我有一个在端口80上侦听的Web代理,我在IIS上设置了URL重写(web.config上的规则如下),它确实

http://example.com/api/blah/blah - >
http://example.com:8095/api/blah/blah

  <rule name="api-example" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
     <match url="^.*/api/.*$" />
     <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
     <action type="Rewrite" url="http://localhost:8095/{R:0}" appendQueryString="true" logRewrittenUrl="false" />
  </rule> 

它工作正常,没有问题,当我直接请求localhost:8095

时也可以

但我也希望能够识别是直接请求了请求还是通过URL重写模块。

我的想法是,将一个查询字符串附加到URL上,当网址在IIS上重写时,我可以检查查询字符串是否存在,然后通过URL重写,如果没有,那么它是一个直接请求

的示例:

http://example.com/api/blah/blah?from=proxy - &gt; http://example.com:8095/api/blah/blah?from=proxy

http://example.com/api/blah/blah?existing_query_string=blah&from=proxy - &gt; http://example.com:8095/api/blah/blah?existing_query_string=blah&from=proxy

我怎样才能做到这一点?或者还有其他办法吗?

非常感谢

2 个答案:

答案 0 :(得分:1)

我最终选择了自己的解决方案,即在IIS重写url时添加查询字符串 并且Url重写模块足够聪明,可以将查询字符串arr = 1附加到url,无论url是否已经有查询字符串,

然后我添加代码来检查查询字符串是否包含arr = 1,如果有,则请求来自url重写

<rule name="api-example" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
     <match url="^.*/api/.*$" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{QUERY_STRING}" pattern="(.*)" negate="false" />
      </conditions>
     <action type="Rewrite" url="http://localhost:8095/{R:0}?arr=1" appendQueryString="true" logRewrittenUrl="false" />
  </rule> 

答案 1 :(得分:0)

您可以尝试访问

Request.ServerVariables["HTTP_X_ORIGINAL_URL"]

并将其与Request.Url.PathAndQuery进行比较。或者,

Request.RawUrl

还应包含原始网址。