为什么这个IIS7重写执行redirect?

我有一个重写规则与301redirect响应,但我不知道为什么。

我在WinHost共享主机,使用IIS 7重写来托pipe我从我的帐户的子文件夹拥有另一个域。

默认情况下,WinHost将您的其他域指向您帐户的根目录。 所以我的目标是双重的:

  1. 我想保持其他域文件在物理上分开在一个子文件夹中
  2. 我想隐藏地址栏中的所述子文件夹

以便:

http://myotherdomain.com

获取服务:

E:\account_root\myotherdomain

所以使用IIS 7重写模块,我产生了这个规则。

<xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="Test" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="(^|\.)myotherdomain\.com$" /> </conditions> <action type="Rewrite" url="myotherdomain/{R:1}" /> </rule> </rules> </rewrite> </system.webServer> </configuration>

我想请求myotherdomain.com抵消到myotherdomain子文件夹。

这是可行的,但只有在请求中没有path的情况下。 如果我请求http://myotherdomain.com我收到200响应&在我的浏览器中,我看到位于E:\account_root\myotherdomain的默认文档。 不发生redirect。

如果您添加请求的path,例如http://myotherdomain.com/test ,现在我收到301redirect到重写的URL:

Response: HTTP/1.1 301 Moved Permanently

Location: http://myotherdomain.com/myotherdomain/test/

浏览器然后获取:

Request: GET /myotherdomain/test/ HTTP/1.1

然后,重写规则再次在IIS上运行,并最终IIS尝试提供位于以下位置的默认文档:

E:\account_root\myotherdomain\myotherdomain\test

哪些不存在:

Response: HTTP/1.1 404 Not Found

所以重写本身似乎在工作; 我不明白的是为什么IIS将301redirect投入混合,但只有在请求中存在path时。

是不是redirect添加尾随/?

您的规则看起来是正确的,并且按照您的设想重写而不是redirect。 这听起来像你的网站上的其他东西导致301客户端redirect。 尝试testing一个.htm页面,看它是否是执行redirect的aspx或htm。 您的URL重写规则不会导致这种情况。

你在正确的轨道上。 我有一些关于这种情况的博客post,包括即将离任的规则:

  • 传入规则 (你已经有了)
  • 输出部分