Rahul Dagli
Rahul Dagli

Reputation: 4502

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. I'm using Craft CMS running on IIS 7 server.

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

enter image description here

Upvotes: 0

Views: 835

Answers (1)

Kaushal Kumar Panday
Kaushal Kumar Panday

Reputation: 2467

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.

<?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

If you need to troubleshoot URL Rewrite rules, then enable Failed Request tracing for your site. 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.

Upvotes: 1

Related Questions