首页 > 解决方案 > 使用 web.config 规则重定向到子域

问题描述

我们迁移托管在主域内的目录“webapp”中的 webapp:

http://www.example.com/webapp/

到没有“webapp”文件夹的新子域:

http://subdomain.example.com/

我只需要重定向子目录“webapp”。我不想重定向其他网址,如http://www.example.com/, http://www.example.com/folder1, http://www.example.com/folder2, ....

但是我们已经向客户发送了很多电子邮件,其中包含以下链接:<a href="http://www.example.com/webapp/login">LOGIN TO YOUR DASHBOARD</a>或其他链接,例如<a href="http://www.example.com/webapp/data/download.php/id=xxx">DOWNLOAD YOUR DOC</a>

现在我正在尝试通过 web.config 进行重定向,但似乎没有任何效果。

例如,如果我尝试不导航到,http://www.example.com/webapp/login我不会http://subdomain.example.com/login/按预期重定向到。

例如,如果我尝试不导航到,http://www.example.com/webapp/data/download.php/id=43我不会http://subdomain.example.com/data/download.php/id=43按预期重定向到。

有什么建议吗?

<rule name="redirect-to-subdomain" stopProcessing="true">
    <match url="^webapp/$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Redirect" url="http://subdomain.example.com/{R:0}" redirectType="Permanent" />
</rule>

标签: asp.netredirectiisweb-config

解决方案


除了http://www.example.com/webapp/之外,您的规则将不起作用。

如果您想重定向链接,例如http://www.example.com/webapp/login。请试试这个规则。

<rule name="redirect-to-subdomain" stopProcessing="true">
    <match url="^webapp/(.*)$" />
    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Redirect" url="http://subdomain.example.com/{R:1}" redirectType="Temporary" />
</rule>

推荐阅读