简体   繁体   English

如何在IIS中使用web.config将特定的子域重定向到主域?

[英]How to redirect a specific sub-domain to a main domain using in IIS using web.config?

Currently I have a sub-domain https://blog.example.com which I would like to redirect to https://example.com/blog . 目前,我有一个子域https://blog.example.com ,我想将其重定向到https://example.com/blog I'm using Craft CMS running on IIS 7 server. 我正在使用在IIS 7服务器上运行的Craft CMS。

I tried the code mentioned below but it didn't work: 我尝试了下面提到的代码,但是没有用:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
         <rewrite>
             <rules>
                <rule name="Redirect blog to new url" enabled="true" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{HTTP_HOST}" pattern=".*" />
                    </conditions>
                    <action type="Redirect" url="https://example.com/blog/{R:0}" />
                </rule>
             </rules>
         </rewrite>
    </system.webServer>
</configuration>

Update: Adding directory structure 更新:添加目录结构

在此处输入图片说明

The problem seems to be with the condition you have specified. 问题似乎出在您指定的条件上。 Your current pattern is .* which will result in a infinite loop. 您当前的模式是。* ,这将导致无限循环。 Change the pattern to check for the specific URL as below. 更改模式以检查特定的URL,如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
         <rewrite>
             <rules>
                <rule name="Redirect blog to new url" enabled="true" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{HTTP_HOST}" pattern=".*blog\.example\.com.*" />
                    </conditions>
                    <action type="Redirect" url="https://example.com/blog/{R:0}" />
                </rule>
             </rules>
         </rewrite>
    </system.webServer>
</configuration>

I tried this and it works for me. 我尝试了这个,对我有用。 Go to http://blog.kaushal.co.in 前往http://blog.kaushal.co.in

If you need to troubleshoot URL Rewrite rules, then enable Failed Request tracing for your site. 如果您需要对URL重写规则进行故障排除, 为您的站点启用失败请求跟踪 See this for more details: Using Failed Request Tracing to Trace Rewrite Rules 请参阅此以获取更多详细信息: 使用失败的请求跟踪来跟踪重写规则

UPDATE 更新

The rewrite rules need to be added to the 117072616095252 present at the site's root "/" and not in a web.config of one of the child folders. 重写规则需要添加到站点根目录“ /”而不是子文件夹之一的web.config中的117072616095252中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM