How do you create ARR URL rewrite conditions via powershell [Answered]RSS

8 replies

Last post Feb 19, 2020 01:31 AM by Jalpa Panchal

  • How do you create ARR URL rewrite conditions via powershell

    Feb 05, 2020 09:20 PM|trevoryour_cp|LINK

    I'm trying to write a script that will create our ARR redirect for us when we supply a sitename.  It works for the most part but instead of adding a condition to an existing rule, it wipes out the conditions and only lists the one I just added.  There has to be a way to add a conditions to a rule without wiping out the other conditions.  Ideally I would like to be able to supply a site url and it will be added to the list of other URLs in the condition list and set it to "match any" for those conditions.

    Here is my code.  

    $sitename = "Site7"
    $name = "'ARR Rule'"
    $URL = ".domain.com"
    $site = 'IIS:\Sites\Default Web Site'
    
    Set-WebConfigurationProperty -PSPath $site -filter "system.webServer/rewrite/rules/rule[@name=$name]/conditions" -name '.' -value @{input='{HTTP_HOST}'; matchType='0'; pattern=$sitename+$URL; ignoreCase='True'; negate='False'};

  • Re: How do you create ARR URL rewrite conditions via powershell

    Feb 06, 2020 02:20 AM|Jalpa Panchal|LINK

    Hi,

    Below is the sample script to create URL rewrite rule using PowerShell:

    Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/app2'  -filter "system.webServer/rewrite/rules" -name "." -value @{name='ARR Rule';stopProcessing='True'}
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/app2'  -filter "system.webServer/rewrite/rules/rule[@name='ARR Rule']/match" -name "url" -value "(.*)"
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/app2'  -filter "system.webServer/rewrite/rules/rule[@name='ARR Rule']/conditions" -name "logicalGrouping" -value "MatchAny"
    
    Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/app2'  -filter "system.webServer/rewrite/rules/rule[@name='ARR Rule']/conditions" -name "." -value @{input='{HTTP_HOST}';pattern='^www.domain.com$'}
    
    Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/app2'  -filter "system.webServer/rewrite/rules/rule[@name='ARR Rule']/conditions" -name "." -value @{input='{HTTPS}';pattern='off'}
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/app2'  -filter "system.webServer/rewrite/rules/rule[@name='ARR Rule']/action" -name "type" -value "Redirect"
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/app2'  -filter "system.webServer/rewrite/rules/rule[@name='ARR Rule']/action" -name "url" -value "http://www.test.com/"
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/app2'  -filter "system.webServer/rewrite/rules/rule[@name='ARR Rule']/action" -name "logRewrittenUrl" -value "True"
    

    and by using appcmd:

    appcmd.exe set config "app2" -section:system.webServer/rewrite/rules /+"[name='ARR Rule',stopProcessing='True']" 
    appcmd.exe set config "app2" -section:system.webServer/rewrite/rules /"[name='ARR Rule',stopProcessing='True'].match.url:"(.*)""  
    appcmd.exe set config "app2" -section:system.webServer/rewrite/rules /"[name='ARR Rule',stopProcessing='True'].conditions.logicalGrouping:"MatchAny""  
    
    appcmd.exe set config "app2" -section:system.webServer/rewrite/rules /+"[name='ARR Rule',stopProcessing='True'].conditions.[input='{HTTP_HOST}',pattern='^www.domain.com$']" 
    
    appcmd.exe set config "app2" -section:system.webServer/rewrite/rules /+"[name='ARR Rule',stopProcessing='True'].conditions.[input='{HTTPS}',pattern='off']" 
    appcmd.exe set config "app2" -section:system.webServer/rewrite/rules /"[name='ARR Rule',stopProcessing='True'].action.type:"Redirect"" /"[name='ARR Rule',stopProcessing='True'].action.url:"http://www.test.com/"" /"[name='ARR Rule',stopProcessing='True'].action.logRewrittenUrl:"True""  
    

    or

    $site = "iis:\sites\Default Web Site"
    $filterRoot = "system.webServer/rewrite/rules/rule[@name='fddapi$_']"
    Clear-WebConfiguration -pspath $site -filter $filterRoot
    Add-WebConfigurationProperty -pspath $site -filter "system.webServer/rewrite/rules" -name "." -value @{name='fddapi' + $_ ;patternSyntax='Regular Expressions';stopProcessing='False'}
    Set-WebConfigurationProperty -pspath $site -filter "$filterRoot/match" -name "url" -value "(fddapi)/.*"
    Set-WebConfigurationProperty -pspath $site -filter "$filterRoot/conditions" -name "logicalGrouping" -value "MatchAny"
    Set-WebConfigurationProperty -pspath $site -filter "$filterRoot/action" -name "type" -value "Rewrite"
    Set-WebConfigurationProperty -pspath $site -filter "$filterRoot/action" -name "url" -value "http://localhost:8081/{R:0}"

    you could modify the value based on your requirements. 

    Regards,

    Jalpa

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
  • Re: How do you create ARR URL rewrite conditions via powershell

    Feb 06, 2020 02:02 PM|trevoryour_cp|LINK

    Looking at the I still don't see where this modifies and existing rule.  The Add-Webconfigurationproperty command creates a new rule I believe.  Can you point out the part of the script that modifies an existing record?  

  • Re: How do you create ARR URL rewrite conditions via powershell

    Feb 06, 2020 09:33 PM|trevoryour_cp|LINK

    I got it.  My mistake was always using the Set-Webconfigurationproperty instead of Add-Webconfigurationproperty.

    The script works 100% except its putting the ARR rules under Default Web Site.  All of our URL rewrite rules are at the root level.  Is it possible to push these setting to the root URL rewrite rules?

  • Re: How do you create ARR URL rewrite conditions via powershell

    Feb 07, 2020 02:39 AM|Jalpa Panchal|LINK

    Did you mean you want to set this rule at the server level or default site? if you want to move to the default site from another site you need to do it manually by running the script and change the name of the site. 

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
  • Re: How do you create ARR URL rewrite conditions via powershell

    Feb 07, 2020 02:07 PM|trevoryour_cp|LINK

    No.  The URL rewrite rules exist on the server level.  There are no rules set at the default site level.  My script will create URL rewrite rules at the default site level but when I try to use:

    $root = 'system.webServer/rewrite/GlobalRules' 

    instead of:

    $root = 'system.webServer/rewrite/rules'

    When I try running that I get this error.

    Filename:
    Error: This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set
    explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false".
    + CategoryInfo : NotSpecified: (:) [Add-WebConfigurationProperty], FileLoadException
    + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.IIs.PowerShell.Provider.AddConfigurationPropertyCommand

  • Rovastar Rovastar

    5458 Posts

    MVP

    Moderator

    Re: How do you create ARR URL rewrite conditions via powershell

    Feb 07, 2020 11:20 PM|Rovastar|LINK

    I'm not expert on powershell for this but I think it might be

    system.webServer/rewrite/GlobalRules/rules

    This might give you clues

    https://forums.iis.net/t/1240580.aspx?How+to+change+a+rewrite+rule+name+via+powershell+

    add you prob need to run administrator too.

    Troubleshoot IIS in style
    https://www.leansentry.com/
  • Re: How do you create ARR URL rewrite conditions via powershell

    Feb 10, 2020 02:25 AM|Jalpa Panchal|LINK

    Hi,

    Make sure you run the PowerShell as an administrator.yu could try below rule to apply it at the server level:

    Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.webServer/rewrite/globalRules" -name "." -value @{name='ARR Rule';stopProcessing='True'}
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.webServer/rewrite/globalRules/rule[@name='ARR Rule']/match" -name "url" -value "(.*)"
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.webServer/rewrite/globalRules/rule[@name='ARR Rule']/conditions" -name "logicalGrouping" -value "MatchAny"
    
    Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.webServer/rewrite/globalRules/rule[@name='ARR Rule']/conditions" -name "." -value @{input='{HTTP_HOST}';pattern='^www.domain.com$'}
    
    Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.webServer/rewrite/globalRules/rule[@name='ARR Rule']/conditions" -name "." -value @{input='{HTTPS}';pattern='off'}
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.webServer/rewrite/globalRules/rule[@name='ARR Rule']/action" -name "type" -value "Redirect"
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.webServer/rewrite/globalRules/rule[@name='ARR Rule']/action" -name "url" -value "http://www.test.com/"
    

    you also need to confirm that iis global rule section is unlocked at the server level.

    to check that please follow the below steps:

    1)Open iis select your server name.

    2)open configuration editor.

    3)in configuration editor from section dropdown select: system.webServer/rewrite/globalRules

    4)and from the action, pane click on the unlock section.

    Regards,

    Jalpa

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
  • Re: How do you create ARR URL rewrite conditions via powershell

    Feb 19, 2020 01:31 AM|Jalpa Panchal|LINK

    Hi,

    Is your issue solved?

    If your issue is solved then I request you to mark the helpful suggestion as an answer. This will help other people who face the same issue.

    If your issue still exists then try to refer the solution given by the community members.

    If then also you have any further questions then let us know about it.

    We will try to provide further suggestions to solve the issue.

    Thanks for your understanding.

    Regards

    Jalpa.

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.