Trying to create an IIS URL Redirect

Mik Muller 1 Reputation point
2022-11-04T16:54:02.007+00:00

I am trying to write a rule that will forward the current URL to https if the visitor went to the http URL, as well as if they went to an alternative URL, such as an older domain name. It all seems valid, but it's not working. Any thoughts? What am I missing? Should these be broken into two rules?

`c:\windows\system32\inetsrv\appcmd set config "SITE_1" -section:system.webServer/rewrite/rules /+"[name='https_SITE_1',enabled='True',stopProcessing='True']"

c:\windows\system32\inetsrv\appcmd set config "SITE_1" -section:system.webServer/rewrite/rules /[name='https_SITE_1'].match.url:"^(.*)$"

c:\windows\system32\inetsrv\appcmd set config "SITE_1" -section:system.webServer/rewrite/rules /+"[name='https_SITE_1'].conditions.[input='{HTTP_HOST}',pattern='^oldsite.com$']"

c:\windows\system32\inetsrv\appcmd set config "SITE_1" -section:system.webServer/rewrite/rules /[name='https_SITE_1'].action.type:'Redirect' /[name='https_SITE_1'].action.url:"https://newsite.com/{R:1}" /[name='https_SITE_1'].action.redirectType:'Permanent'`

Internet Information Services
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Yurong Dai-MSFT 2,811 Reputation points Microsoft Vendor
    2022-11-07T03:08:52.03+00:00

    Hi @Mik Muller ,

    Did you run successfully using the command line above? I also tested with the command you provided, but I didn't run it successfully and got the following error message:

    257694-image.png

    Because the command line for setting action.type is incorrect, the rule is created incomplete and does not work. I modified the command line and ran it again, and it was successful:

    appcmd.exe set config "SITE_1" -section:system.webServer/rewrite/rules /+"[name='https_SITE_1',enabled='True',stopProcessing='True']"   
    appcmd.exe set config "SITE_1" -section:system.webServer/rewrite/rules /[name='https_SITE_1'].match.url:"^(.*)$"   
    appcmd.exe set config "SITE_1" -section:system.webServer/rewrite/rules /+"[name='https_SITE_1'].conditions.[input='{HTTP_HOST}',pattern='^oldsite.com$']"   
    appcmd.exe set config "SITE_1" -section:system.webServer/rewrite/rules /"[name='https_SITE_1'].action.type:"Redirect"" /"[name='https_SITE_1'].action.url:"https://newsite.com/{R:1}"" /"[name='https_SITE_1'].action.redirectType:"Permanent""  
    

    257686-image.png

    At this point, you can see the successfully created rule code block in the web.config file:

    257676-image.png

    If the visitor goes to an http URL, and if they go to an alternate URL (such as an old domain name), forward the current URL to https
    According to your description, I think one more condition can be added, <add input="{HTTPS}" pattern="^OFF$" />, the complete rule is as follows:

    <rewrite>  
                <rules>  
                    <rule name="https_SITE_1" enabled="true" stopProcessing="true">  
                        <match url="^(.*)$" />  
                        <conditions logicalGrouping="MatchAll">  
                            <add input="{HTTP_HOST}" pattern="^oldsite.com$" />  
                            <add input="{HTTPS}" pattern="^OFF$" />  
                        </conditions>  
                        <action type="Redirect" url="https://newsite.com/{R:1}" redirectType="Permanent" />  
                    </rule>  
                </rules>  
            </rewrite>  
    

    Update answer:

    I saw your reply, below I reply how to create it from appcmd.

    web.config

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="rewrite_NEWSITE" enabled="true" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="^oldsite.com$" /> </conditions> <action type="Redirect" url="https://newsite.com/{R:1}" redirectType="Found" /> </rule> <rule name="https_NEWSITE" enabled="true"> <match url="(.*)" ignoreCase="true" /> <conditions> <add input="{HTTPS}" pattern="off" /> </conditions> <action type="Redirect" url="https://newsite.com/{R:1}" redirectType="Found" /> </rule> </rules> </rewrite> </system.webServer> </configuration>

    appcmd

    Run cmd as administrator, and first change the path to where appcmd.exe is located (C:windows\system32\inetsrv)

    cd %windir%\system32\inetsrv  
    

    Then run the following command line code together: (The site name is SITE_1)

    appcmd.exe set config "SITE_1" -section:system.webServer/rewrite/rules /+"[name='rewrite_NEWSITE',enabled='True',stopProcessing='True']"   
    appcmd.exe set config "SITE_1" -section:system.webServer/rewrite/rules /[name='rewrite_NEWSITE'].match.url:"(.*)"   
    appcmd.exe set config "SITE_1" -section:system.webServer/rewrite/rules /+"[name='rewrite_NEWSITE'].conditions.[input='{HTTP_HOST}',pattern='^oldsite.com$']"   
    appcmd.exe set config "SITE_1" -section:system.webServer/rewrite/rules /"[name='rewrite_NEWSITE'].action.type:"Redirect"" /"[name='rewrite_NEWSITE'].action.url:"https://newsite.com/{R:1}"" /"[name='rewrite_NEWSITE'].action.redirectType:"Found""  
    appcmd.exe set config "SITE_1" -section:system.webServer/rewrite/rules /+"[name='https_NEWSITE',enabled='True']"   
    appcmd.exe set config "SITE_1" -section:system.webServer/rewrite/rules /[name='https_NEWSITE'].match.url:"(.*)"   
    appcmd.exe set config "SITE_1" -section:system.webServer/rewrite/rules /+"[name='https_NEWSITE'].conditions.[input='{HTTPS}',pattern='off']"   
    appcmd.exe set config "SITE_1" -section:system.webServer/rewrite/rules /"[name='https_NEWSITE'].action.type:"Redirect"" /"[name='https_NEWSITE'].action.url:"https://newsite.com/{R:1}"" /"[name='https_NEWSITE'].action.redirectType:"Found""  
    

    The test result in the image below is successful.
    258042-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the email notification for this thread.

    Best regards,
    Yurong Dai

    0 comments No comments

  2. Mik Muller 1 Reputation point
    2022-11-07T16:14:46.707+00:00

    Ok, I got web.config working, now using two rules for the two considerations, but am not sure how to create it from the appcmd.

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="rewrite_NEWSITE" enabled="true" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="^oldsite.com$" /> </conditions> <action type="Redirect" url="https://newsite.com/{R:1}" redirectType="Found" /> </rule> <rule name="https_NEWSITE" enabled="true"> <match url="(.*)" ignoreCase="true" /> <conditions> <add input="{HTTPS}" pattern="off" /> </conditions> <action type="Redirect" url="https://newsite.com/{R:1}" redirectType="Found" /> </rule> </rules> </rewrite> </system.webServer> </configuration>


  3. Mik Muller 1 Reputation point
    2022-11-08T20:16:16.61+00:00

    Sorry, that didn't work. Still missing stuff. I'll just create the web.config file manually and drop it where it needs to be now that I found the right info to put in there. Thanks for your help, Yurong.

    0 comments No comments