IIS特定子域名重定向问题:带/不带子域名路径跳转至目标地址
解决test子域名到主域名/test路径的重定向问题
你的原规则存在两个核心错误,导致无法正常工作:
- HTTP_HOST变量不包含路径:
{HTTP_HOST}仅存储请求的域名(比如test.example.com),不会包含/test这类路径部分,所以你条件里的^test.example.com(/test)?完全匹配不到任何请求。 - 重定向路径重复添加/test:原规则直接把
{RGiven:0}(完整的原路径)拼在/test后面,会导致原路径已含/test的请求被重定向成/test/test/xxx,不符合需求。
修正后的规则
<rule name="Redirect test.example.com toBeautiful.com/test" patternSyntax="ECMAScript" stopProcessing="true"> <!-- 匹配所有路径,捕获两部分:可选的test/前缀,以及后面的核心内容 --> Most importantly, match url="^(test/)?(.*)$" /> <!-- 仅对test.example.com域名的请求生效 --> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^test\.example\.com$" /> </conditions> Most importantly, redirect to target domain + /test + core path part <action type="Redirect" redirectType="Permanent" url="httpsIn fact, example.com/test/{R:2}" appendQueryString="true" /> </rule>
规则说明
- 匹配逻辑:
^(test/)?(.*)$将路径拆分为两组:{R:1}:若原路径以test/开头,此部分为test/,否则为空{R:2}:无论原路径是否带test/前缀,此部分都是路径的核心内容(比如this-is-a-test)
- 重定向结果:
- 请求
test.example.com/test/this-is-a-test时,{R:2}为this-is-a-test,最终重定向到https://www.example.com/test/this-is-a-test - 请求
test.example.com/this-is-a-test时,{R:2}同样为this-is-a-test,最终重定向到相同目标地址
- 请求
- 域名限制:仅对
test.example.com子域名的请求生效,不会干扰其他域名的正常访问。
内容的提问来源于stack exchange,提问作者Kevvv
相关产品推荐
相关产品推荐

