locked
How to add URL Rewrite using command line RRS feed

  • Question

  • User-1727828520 posted

    I searched many places but nowhere is there a way to create a rewrite url using cmd.
    Someone please help me.

    Tuesday, January 15, 2019 2:56 AM

Answers

  • User-848649084 posted

    Hi trongthai,

    To add match url in rewrite rule using appcmd use below command:

    • appcmd.exe set config "site1" -section:system.webServer/rewrite/rules /"[name='Redirect to HTTPS'].match.url:"(client*)""
    • appcmd.exe set config "site1" -section:system.webServer/rewrite/rules /+"[name='Redirect to HTTPS'].conditions.[input='{REQUEST_FILENAME}',matchType='IsFile']"
    • appcmd.exe set config "site1" -section:system.webServer/rewrite/rules /+"[name='Redirect to HTTPS'].conditions.[input='{REQUEST_FILENAME}',matchType='IsDirectory']"
    • appcmd.exe set config "site1" -section:system.webServer/rewrite/rules /"[name='Redirect to HTTPS'].action.type:"Rewrite""
    • appcmd.exe set config "site1" -section:system.webServer/rewrite/rules /"[name='Redirect to HTTPS'].action.url:"client/index.html""

    output:

    <rule name="Redirect to HTTPS">
    <match url="(client*)" />
    <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
    </conditions>
    <action type="Rewrite" url="client/index.html" />
    </rule>

    You could also refer https://forums.iis.net/t/1240029.aspx post.

    Regards,

    Jalpa.

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, January 15, 2019 7:07 AM
    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Wednesday, January 16, 2019 6:03 PM

All replies

  • User-848649084 posted

    Hi trongthai,

    To add match url in rewrite rule using appcmd use below command:

    • appcmd.exe set config "site1" -section:system.webServer/rewrite/rules /"[name='Redirect to HTTPS'].match.url:"(client*)""
    • appcmd.exe set config "site1" -section:system.webServer/rewrite/rules /+"[name='Redirect to HTTPS'].conditions.[input='{REQUEST_FILENAME}',matchType='IsFile']"
    • appcmd.exe set config "site1" -section:system.webServer/rewrite/rules /+"[name='Redirect to HTTPS'].conditions.[input='{REQUEST_FILENAME}',matchType='IsDirectory']"
    • appcmd.exe set config "site1" -section:system.webServer/rewrite/rules /"[name='Redirect to HTTPS'].action.type:"Rewrite""
    • appcmd.exe set config "site1" -section:system.webServer/rewrite/rules /"[name='Redirect to HTTPS'].action.url:"client/index.html""

    output:

    <rule name="Redirect to HTTPS">
    <match url="(client*)" />
    <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
    </conditions>
    <action type="Rewrite" url="client/index.html" />
    </rule>

    You could also refer https://forums.iis.net/t/1240029.aspx post.

    Regards,

    Jalpa.

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, January 15, 2019 7:07 AM
  • User-1727828520 posted

    Thanks Jalpa Panchal,

    That's exactly what I need.

    Thank you so much.

    Tuesday, January 15, 2019 7:13 AM
  • User-848649084 posted

    Hi,

    From your post, I can see that your issue in the original post is solved now. i request you to mark the helpful suggestion as an answer. 

    Thank you for understanding.

    Regards,

    Jalpa.

    Tuesday, January 15, 2019 7:18 AM
  • User-1727828520 posted

    In case I created a new website and didn't have a web.config file, what should I do?

    Tuesday, January 15, 2019 9:52 AM
    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Wednesday, January 16, 2019 6:03 PM
  • User-1727828520 posted

    I tried this and got an error:

    'Add-WebConfigurationProperty' is an internal command or external command,
    It is not recognized as an operable program or batch file.

    Thursday, January 17, 2019 2:06 AM
  • User690216013 posted
    1. Write a PowerShell script like the blog post said.
    2. Call this script at command prompt.

    I am not asking you to call "Add-WebConfigurationProperty" at command prompt, as that obviously won't work.

    Thursday, January 17, 2019 2:58 PM