0

如果标题不清楚,请道歉,基本上我有一个监听端口 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 上,当 url 在 IIS 上被重写时,我可以检查查询字符串是否存在,如果不存在,则通过 URL 重写,那么这是直接请求

例子:

http://example.com/api/blah/blah?from=proxy -> http://example.com:8095/api/blah/blah?from=proxy

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

我怎样才能做到这一点?还是有其他方法可以做到这一点?

非常感谢

4

2 回答 2

1

我最终找到了自己的解决方案,即在 IIS 进行 url 重写时添加一个查询字符串,并且 Url 重写模块足够聪明地将查询字符串 arr=1 附加到 url 与 url 是否已经有查询字符串无关,

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

<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> 
于 2013-12-13T15:33:51.497 回答
0

您可以尝试访问

Request.ServerVariables["HTTP_X_ORIGINAL_URL"]

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

Request.RawUrl

还应包含原始 URL。

于 2013-12-12T17:38:03.113 回答