0

我正在尝试重写 url 以更改请求,例如

http://foo.bar.com/

http://bar.com/foo/

我已经像这样将我的 URL 重写规则弹出到 Web 配置中

<rewrite>
  <rules>
      <rule name="SiteReWrite" stopProcessing="true">
          <match url="(.*)" />
          <action type="Rewrite" url="http://bar.com/{C:1}/"/>
          <conditions>
              <add input="{HTTP_HOST}" pattern="(.*)\.bar\.com" />
          </conditions>
      </rule>
  </rules>
</rewrite>

但我收到 404.4 错误。查看跟踪日志文件,我在其中看到了这个

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
 <System>
  <Provider Name="WWW Server" Guid="{3A2A4E84-4C21-4981-AE10-3FDA0D9B0F83}"/>
  <EventID>0</EventID>
  <Version>1</Version>
  <Level>4</Level>
  <Opcode>42</Opcode>
  <Keywords>0x0</Keywords>
  <TimeCreated SystemTime="2012-12-03T05:54:01.237Z"/>
  <Correlation ActivityID="{00000000-0000-0000-1000-0080030000FC}"/>
  <Execution ProcessID="7312" ThreadID="3180"/>
  <Computer>ULTRA</Computer>
 </System>
 <EventData>
  <Data Name="ContextId">{00000000-0000-0000-1000-0080030000FC}</Data>
  <Data Name="OldUrl">/</Data>
  <Data Name="NewUrl">http://bar.com/foo/</Data>
 </EventData>
 <RenderingInfo Culture="en-US">
  <Opcode>URL_CHANGED</Opcode>
 </RenderingInfo>
 <ExtendedTracingInfo xmlns="http://schemas.microsoft.com/win/2004/08/events/trace">
  <EventGuid>{D42CF7EF-DE92-473E-8B6C-621EA663113A}</EventGuid>
 </ExtendedTracingInfo>
</Event>

从 NewUrl 看起来一切都很美好。注意,看起来这个实例被缓存了,但我在之前的事件中看到了规则匹配。

如果我输入网址http://bar.com/foo/它应该可以正常工作。我不希望用户看到新的 url,他们应该认为他们在 foo.bar.com 而不是 bar.com/foo。这只是我在 mvc 应用程序中的一些路由。

有谁知道它为什么会出现这种奇怪的行为?我为此使用 IIS express 8,我还没有尝试使用 IIS。

4

2 回答 2

0

你应该redirect而不是rewrite

<rewrite>
  <rules>
      <rule name="SiteReWrite" stopProcessing="true">
          <match url="(.*)" />
          <action type="Redirect" url="http://bar.com/{C:1}/"/>
          <conditions>
              <add input="{HTTP_HOST}" pattern="(.*)\.bar\.com" />
          </conditions>
      </rule>
  </rules>
</rewrite>

如果您真的想重写,您必须通过安装 ARR(应用程序请求路由)模块将 IIS 设置为反向代理。

于 2012-12-03T11:43:45.910 回答
0

由于我不希望用户看到 url 更改,我无法使用重定向。但是我应该提到的是所有子域都将访问同一个站点。一旦我想了更多,并且对重写模块进行了更多阅读,我修改了一个这样的 MSDN 示例;这就像一个魅力

<rewrite>
  <rules>
    <rule name="Rewrite subdomain">
      <match url="(.*)" /> <!-- rule back-reference is captured here -->
      <conditions>
        <add input="{HTTP_HOST}" pattern="^([^.]+)\.bar\.com$" /> <!-- condition back-reference is captured here -->
      </conditions>
      <action type="Rewrite" url="{C:1}/{R:1}" /> <!-- rewrite action uses back-references to condition and to rule when rewriting the url -->
    </rule> 
  </rules>
</rewrite>
于 2012-12-04T03:40:47.187 回答