IIS 7 URL 重写和两个域
我正在写这个问题,在这个过程中,它迫使我更加努力地思考,我自己回答了它,尽管我仍然不完全明白它为什么解决它。
我在共享主机上有一个帐户,注册了 2 个域。 我正在使用 Asp.Net 堆栈来运行一些内容,例如博客和我计划最终启动的另一个网站。 我的两个域都指向根; 第一个是我用来注册的原始文件,第二个是我添加的根域指针。 这是我希望它的行为方式:
目录结构:
Root (www.domain1.com) Root --\ Blog (www.domain1.com/blog) Root --\ Site2 (should be directed here if www.domain2.com) Root --\ Site2 --\ Junk (www.domain2.com/junk)
现在,如果您输入 www.domain1.com 或 www.domain1.com/blog,它的行为会按预期进行,我对此很满意。 对于 www.domain2.com,我配置了这样的重写规则(来自 web.config):
<rule name="Domain2">
<match url="(.*)(/)?" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="(www\.)?domain2\.com" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="/site2/{R:1}" />
</rule>
如果主机是domain2.com,则该规则应该匹配任何路径,选择请求资源的路径并对其进行格式化适当地。 因此,当有人在 IIS 中输入 www.domain2.com/junk/default.aspx 时,它会解析为 www.domain2.com/site2/junk/default.aspx,而用户却毫不知情。 除非用户不在子文件夹中键入尾部斜杠,否则这大部分都按照广告进行工作。 IE:
www.domain2.com(有效)
www.domain2.com/(有效)
www.domain2.com/junk/(有效)
www.domain2.com/junk(不起作用!) IIS 7 在这里失去了理智,将其格式化为像 www.domain2.com/site2/junk 一样,因为会自动针对尾部斜杠发出第二个请求,并发生 404。
所以,我将操作更新为:
<action type="Rewrite" url="/site2/{R:1}/" />
这似乎已经解决了它,但为什么 IIS 7 现在不吐出 www.domain2.com/junk2/default.aspx/ ? 它如何知道不要在文档扩展名后添加尾部斜杠?
I was writing up this question and in the process, it forced me to think a little harder and I answered it myself, though I still don't completely understand why it solved it.
I have an account on a shared host with 2 domains registered. I'm using the Asp.Net stack to run a few things like a blog and another site I am planning to kick off eventually. Both of my domains point to the root; the first is the original I used to signup, the second is a root domain pointer I added. Here is how I want it to behave:
Directory Structure:
Root (www.domain1.com) Root --\ Blog (www.domain1.com/blog) Root --\ Site2 (should be directed here if www.domain2.com) Root --\ Site2 --\ Junk (www.domain2.com/junk)
Right now, if you type in www.domain1.com or www.domain1.com/blog, that behaves as expected and I am fine with that. For www.domain2.com, I have the rewrite rule configured like this(from the web.config):
<rule name="Domain2">
<match url="(.*)(/)?" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="(www\.)?domain2\.com" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="/site2/{R:1}" />
</rule>
That rule is supposed to match any path if the host is domain2.com, pick off the path to the requested resource and format it properly. So when somebody types www.domain2.com/junk/default.aspx, in IIS, this resolves to www.domain2.com/site2/junk/default.aspx without the user ever knowing. This is mostly working as advertised except when the user does not type a trailing slash in a subfolder. IE:
www.domain2.com (works)
www.domain2.com/ (works)
www.domain2.com/junk/ (works)
www.domain2.com/junk (doesn't work!) IIS 7 looses its brain here and formats it out like www.domain2.com/site2/junk because a 2nd request is automatically issued for the trailing slash and a 404 happens.
So, I updated the action to be:
<action type="Rewrite" url="/site2/{R:1}/" />
This seems to have resolved it, but why doesn't IIS 7 now spit out www.domain2.com/junk2/default.aspx/ ? How does it know not to append a trailing slash to a document extension?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
www.domain2.com/junk 不起作用,因为您在匹配中添加了斜杠,但要匹配的实际 url 不包含它。 它只是包含“垃圾”。
您还需要添加:
这样,如果找到匹配项,它就不会评估其他规则。 我怀疑您可能会发现让您感到困惑的是您设置的其他规则。
www.domain2.com/junk does not work because you added slash in match, but actual url to match does not contain it. It just contains "junk".
Also you need to add:
<rule name="Domain2" stopProcessing="true">
so it does not evaluate other rules if match is found. I suspect that you may see that what confuses you, is other rules you have setup.