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:
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""
At this point, you can see the successfully created rule code block in the web.config file:
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.
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