Simple URL Rewrite on IIS

可紊 提交于 2019-12-06 12:45:52

问题


I have 1 IIS server that I would like to use to host 2 different web sites (both on port 80).
I've been trying many different combinations (including redirects) and every time, something was breaking (redirect loops, 404, simply not working, etc...)

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}?

Here's the complete 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>

回答1:


I finally figured it out.
It turns out that {REQUEST_URI} is never actually empty, but contains / when there's nothing in it.
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>


来源:https://stackoverflow.com/questions/10159669/simple-url-rewrite-on-iis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!