简体   繁体   English

IIS上的简单URL重写

[英]Simple URL Rewrite on IIS

I have 1 IIS server that I would like to use to host 2 different web sites (both on port 80). 我有1个 IIS服务器,我想用来托管2个不同的网站(都在端口80上)。
I've been trying many different combinations (including redirects) and every time, something was breaking (redirect loops, 404, simply not working, etc...) 我一直在尝试许多不同的组合(包括重定向),每次都会出现问题(重定向循环,404,根本不起作用等等)

The rule I think I need goes something like this: 我认为我需要的规则是这样的:

 - match any URL 
 - condition 1: match {HTTP_HOST} to my site URL 
 - condition 2: discard if {REQUEST_URI} is present 
 - action: rewrite URL to /dir1/index.html  

(repeat for site 2)

The problem here seems to be that condition 2 is never true (what should I use to match the absence of {REQUEST_URI} ? 这里的问题似乎是条件2永远不是真的(我应该用什么来匹配{REQUEST_URI}缺席

Here's the complete XML: 这是完整的XML:

<rewrite>
    <rules>
        <rule name="RuleForSite1" stopProcessing="true">
            <match url="(.*)" ignoreCase="false" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www\.mysite1\.com$" /> 
                <add input="{REQUEST_URI}" pattern=".+" negate="true" /> 
            </conditions>
            <action type="Rewrite" url="dir1/index.html" />
        </rule>
        <rule name="RuleForSite2" stopProcessing="true">
            <match url="(.*)" ignoreCase="false" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www\.mysite2\.com$" /> 
                <add input="{REQUEST_URI}" pattern=".+" negate="true" /> 
            </conditions>
            <action type="Rewrite" url="dir2/index.html" />
        </rule>
    </rules>
</rewrite>

I finally figured it out. 我终于弄明白了。
It turns out that {REQUEST_URI} is never actually empty, but contains / when there's nothing in it. 事实证明, {REQUEST_URI}实际上从来都不是空的,而是包含/什么时候什么也没有。
I also found that a redirect worked out better. 我还发现重定向更好。

This is my final set-up: 这是我的最终设置:

<rewrite>
    <rules>
        <rule name="RuleForSite1" stopProcessing="true">
            <match url="(.*)" ignoreCase="false" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www\.mysite1\.com$" /> 
                <add input="{REQUEST_URI}" pattern="^/$"" /> 
            </conditions>
            <action type="Redirect" url="dir1/index.html" />
        </rule>
        <rule name="RuleForSite2" stopProcessing="true">
            <match url="(.*)" ignoreCase="false" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www\.mysite2\.com$" /> 
                <add input="{REQUEST_URI}" pattern="^/$"" /> 
            </conditions>
            <action type="Redirect" url="dir2/index.html" />
        </rule>
    </rules>
</rewrite>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM