首页 > 解决方案 > IIS - 端口重定向 - 重写模块 - Web.Config 文件

问题描述

我正在尝试创建一个适当的 web.config 文件来重定向最终用户,以向请求添加端口。

例如,输入 http://subscribe.service.com/lists/index.html 的任何最终用户都将被重定向到http://subscribe.service.com:3000/lists/index.html这样无论调用什么请求它将添加到其中:3000。

下面提供了我正在使用的代码示例。在验证测试中,它似乎可以按我的意愿工作。实际上,当我访问物理网站时,什么都没有发生。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpRedirect enabled="false" destination="" childOnly="true" httpResponseStatus="Permanent" />
        <rewrite>
            <rules>
                <rule name="3000 Port Redirect" enabled="true" stopProcessing="true">
                    <match url="http:/(.*)com" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="\.com(.*)" />
                    </conditions>
                    <action type="Redirect" url="{R:0}:3000{C:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

标签: iismoduleurl-rewritingweb-config

解决方案


正如 Lex Li 的文章所提到的,url rewrite model partern 与域不匹配。这意味着 IIS 使用 lists/index.html 来匹配“http://(.*)com”规则。所以你会发现你的规则不起作用。

我建议你可以尝试使用下面的 url 重写规则。

  <rule name="rewrite to 81 port" enabled="true">
                <match url="(.*)" />

                <action type="Redirect" url=" http://subscribe.service.com:3000/{R:0}"  appendQueryString="true"/>
            </rule>

然后它会很好地工作。


推荐阅读