IIS10配置:指定域名指向特定子文件夹及域名重定向需求
IIS 7.5 域名子目录映射及重定向配置方案
针对你的需求,需配置两条URL重写规则实现目标:一是将example.com下指定的reports子目录永久重定向到examplegroup.com对应路径;二是确保examplegroup.com仅对指定的3个reports子文件夹生效。
规则1:将example.com的指定reports子目录重定向到examplegroup.com
这条规则用于统一访问入口,避免重复内容,当用户访问example.com/reports2021、example.com/reports2022等路径时,自动跳转到examplegroup.com的对应地址。
配置代码如下:
<rule name="Redirect Reports to examplegroup.com" stopProcessing="true"> <!-- 匹配以指定3个reports文件夹开头的所有路径 --> <match url="^(reports2021|reports2022|reports2023)(.*)$" ignoreCase="true" /> <conditions> <!-- 仅对example.com域名的请求生效 --> <add input="{HTTP_HOST}" pattern="^example\.com$" /> </conditions> <!-- 301永久重定向到目标域名的对应路径 --> <action type="Redirect" url="https://examplegroup.com/{R:1}{R:2}" redirectType="Permanent" /> </rule>
{R:1}对应匹配到的文件夹名称(如reports2021),{R:2}对应文件夹后的子路径(如/detail),确保跳转后路径完整。redirectType="Permanent"采用301重定向,符合SEO最佳实践。
规则2:限定examplegroup.com仅访问指定reports子文件夹
这条规则确保examplegroup.com仅开放指定的3个reports文件夹权限,其他路径直接返回404(可根据需求调整动作)。
配置代码如下:
<rule name="Restrict examplegroup.com to Reports Folders" stopProcessing="true"> <match url=".*" ignoreCase="true" /> <conditions logicalGrouping="MatchAll"> <!-- 仅对examplegroup.com域名的请求生效 --> <add input="{HTTP_HOST}" pattern="^examplegroup\.com$" /> <!-- 排除指定的3个reports文件夹,非这些路径的请求触发后续动作 --> <add input="{URL}" pattern="^/(reports2021|reports2022|reports2023)" negate="true" /> </conditions> <!-- 对不符合要求的请求返回404 --> <action type="CustomResponse" statusCode="404" statusReason="Not Found" statusDescription="The requested resource does not exist." /> </rule>
- 若无需限制
examplegroup.com的其他路径访问,可跳过这条规则——因站点已绑定该域名,默认会访问根目录内容。
配置步骤说明
- 确保IIS服务器已安装URL重写模块(IIS 7.5需通过Web平台安装程序手动安装)。
- 打开站点的URL重写配置界面,依次添加上述两条规则;或直接编辑站点根目录下的
web.config文件,将规则代码插入<system.webServer><rewrite><rules>节点内。 - 若使用HTTPS,需将规则中的
http://替换为https://,同时确保examplegroup.com已配置有效SSL证书。
内容的提问来源于stack exchange,提问作者hawthorn
相关产品推荐
相关产品推荐

