我正在开发一个客户端应用程序,其中他们在 ASP.NET 应用程序的根子文件夹中有一个嵌套的仅限HTML的应用程序。
域名仅针对 HTML 应用程序更改,我需要确保将请求路由到新域名。 所以,使用rewriteRules
听起来像是一个完美的工具,但我做错了什么,需要另一双眼睛。
原始 URL 如下所示:
https://example.com/folder1/folder2/app/index.html
新域位于完全不同的服务器上,我希望来自app
文件夹的所有请求都转到新域名。 下面是一个示例:
https://newdomain.com/folder1/folder2/app/index.html
不幸的是,我做错了一些可能很明显的事情...... 我一直在重定向中得到这个 URL。
https://newdomain.com/index.html
这是web.config,显示了我尝试过的内容。 它位于app
文件夹中。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectHtmlApp" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www.)?example.com$" />
</conditions>
<action type="Redirect" url="https://newdomain.com/{R:0}" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
{R:0}
和{R:1}
都返回index.html
。
传递给{R:0} 的内容是相对的。因此,如果您的 web.config 位于/folder1/folder2/app/{here} 中,则不会传递 "folder1/folder2/app/"。如果您的 web.config 规则位于/folder1 上方的站点应用程序的根目录中,那么我错了,但如果我是对的,那么您只需要在目标中包含应用程序根目录的路径。鉴于您上面的内容,
<action type="Redirect"
url="https://newdomain.com/folder1/folder2/app/{R:0}"
redirectType="Permanent" appendQueryString="true" />