IIS Url-Rewrite

2019-08-18 06:10发布

问题:

I'm trying to use IIS7 Url Rewrite module rewrite services.mydomain.com/some-file-here to mydomain.webhost.com/folder/some-file-here

The rule is as follows:

Pattern = ^services.mydomain.com/(.*)$
Action = Rewrite
Rewrite URL = http://mydomain.webhost.com/folder/{R:1}

The problem is IIS keeps giving me 404 not found errors. I've been stuck at this for days now. Any ideas?

回答1:

You have your pattern wrong. It should not include domain name or query string there -- only path without leading slash. See the working rule below:

<rule name="MyRewriteRule" stopProcessing="true">
    <match url="^(some-file-here)$" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^services\.mydomain\.com$" />
    </conditions>
    <action type="Redirect" url="http://mydomain.webhost.com/folder/{R:1}" />
</rule>

The above rule will only trigger if host name is services.mydomain.com. If you do not require such additional condition (which is optional) then just remove these 3 lines: <conditions>...</conditions>

Also, the above rule will only do one specific redirect from services.mydomain.com/some-file-here to mydomain.webhost.com/folder/some-file-here. If you need to redirect ANY file like that, then use this one instead:

<rule name="MyRewriteRule" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^services\.mydomain\.com$" />
    </conditions>
    <action type="Redirect" url="http://mydomain.webhost.com/folder/{R:1}" />
</rule>