Share via


Add a URL rewrite condition on IIS using Powershell

Question

Wednesday, November 28, 2012 10:06 AM

Hello, after searching the net and my powershell help command, i cannot find a solution to my problem.

So, i'm adding a rewrite rule to my website which works fine using following commands (the $_ is the variable  that contains my domainname coming from a csv file): 

Add-WebConfigurationProperty -pspath 'iis:\sites\SITENAME'  -filter "system.webServer/rewrite/rules" -name "." -value @{name='Redirect www.' + $_ ;patternSyntax='Wildcard';stopProcessing='True'}
Set-WebConfigurationProperty -pspath 'iis:\sites\SITENAME'  -filter "system.webServer/rewrite/rules/rule[@name='Redirect www.$_']/match" -name "url" -value "*"
Set-WebConfigurationProperty -pspath 'iis:\sites\SITENAME'  -filter "system.webServer/rewrite/rules/rule[@name='Redirect www.$_']/conditions" -name "logicalGrouping" -value "MatchAny"Set-WebConfigurationProperty -pspath 'iis:\sites\SITENAME'  -filter "system.webServer/rewrite/rules/rule[@name='Redirect www.$_']/action" -name "type" -value "Redirect"
Set-WebConfigurationProperty -pspath 'iis:\sites\SITENAME'  -filter "system.webServer/rewrite/rules/rule[@name='Redirect www.$_']/action" -name "url" -value "http://www.$_/{R:0}"

now i want to add a condition (redirect no www url to www), but i cannot find the way to address the collection which contains the conditions.

I want to add a condition with the following options

input= {HTTP_HOST}
matchtype = 0
pattern = domainname.com

Once i set these manually (using the GUI) i can query them using powershell 

Get-WebConfigurationProperty -PSPath 'IIS:\Sites\SITENAME' -Filter "system.webServer/rewrite/rules/rule[@name='Redirect www.domainname.com']/conditions" -name collection[0].pattern.value

but setting them seems impossible.

Can someone please point me in the right direction?

thanks a million!

All replies (10)

Tuesday, December 4, 2012 11:39 AM âś…Answered | 2 votes

Yes, I tested it on IIS7.5

this is what I did on clean IIS with URLRewrite

Create website:

new-website -name sitename -hostheader www.sitename.com -PhysicalPath c:\windows -ApplicationPool sitename

Create URLRewrite rule:

Add-WebConfigurationProperty -pspath 'iis:\sites\SITENAME'  -filt
er "system.webServer/rewrite/rules" -name "." -value @{name='Redirect www.website.com' ;patternSyntax='Wildcard';stopPro
cessing='True'}

add condition:

$list = @{
 pspath = 'MACHINE/WEBROOT/APPHOST/sitename'
 filter = "/system.webServer/rewrite/rules/rule[@name='Redirect www.website.com']/conditions"
 Value = @{
    input = '{HTTP_HOST}'
    matchType ='0'
    pattern ='DUNNOWHAT.COM'
    ignoreCase ='True'
    negate ='False'
}
}
Add-WebConfiguration @list

and it worked.

Did You refresh IIS manager?


Friday, November 30, 2012 5:48 AM

Hi,

Hope the below thread could help:

Scripting Url Rewrite Rules with WebAdministration

http://forums.iis.net/t/1173635.aspx

URL Rewrite Rules created with Powershell

http://forums.iis.net/t/1190443.aspx

Regards,

Yan Li

Cataleya Li
TechNet Community Support


Monday, December 3, 2012 11:22 AM

Hello Yan Li, thanks for the answer, but both links don't solve my problem.

the rewrite rules code created by a Win2012 IIS 8 generate incompatible code. I suppose IIS7.5 or powershell2 incompatible

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/SITENAME'  -filter "system.webServer/rewrite/rules/rule[@name='customrule2']/conditions/add[@input='{HTTP_HOST}' and @matchType='0' and @pattern=DUNNOWHAT.COM' and @ignoreCase='True' and @negate='False']" -name "pattern" -value "DUNNOWHAT.COM"

I can set all the properties of the rewrite rule, except the condition rules... because they are set in a collection i presume...

Thanks again for any tips in resolving this issue


Monday, December 3, 2012 11:42 AM

try this code:

$list = @{
 pspath = 'MACHINE/WEBROOT/APPHOST/sitename'
 filter = "/system.webServer/rewrite/rules/rule[@name='Redirect www.website.com']/conditions"
 Name = '.'
 Value = @{
    input = '{HTTP_HOST}'
    matchType ='0'
    pattern ='DUNNOWHAT.COM'
    ignoreCase ='True'
    negate ='False'
}
}
Set-WebConfigurationProperty @list

Monday, December 3, 2012 3:41 PM

thank you very much for the reply, but unfortunately no solution

i recieve the following warning:

WARNING: Target configuration object '/system.webServer/rewrite/rules/rule[@name='Redirect www.website.com']/conditions is not found at path 'MACHINE/WEBROOT/APPHOST/SITENAME'.

it's actually the same warning when i try the following command:

set-WebConfigurationProperty -pspath 'iis:\sites\SITENAME'  -filter "system.webServer/rewrite/rules/rule[@name='Redirect www.$_']/conditions" -name "matchType" -value "0"

WARNING: Target configuration object 'system.webServer/rewrite/rules/rule[@name='Redirect www.website.com']/conditions is not found at path 'MACHINE/WEBROOT/APPHOST/SITENAME'.

thanks for searching/thinking with me!


Tuesday, December 4, 2012 8:08 AM

Ok, so how about this? (I checked this and it worked for me):

$list = @{
 pspath = 'MACHINE/WEBROOT/APPHOST/sitename'
 filter = "/system.webServer/rewrite/rules/rule[@name='Redirect www.website.com']/conditions"
 Value = @{
    input = '{HTTP_HOST}'
    matchType ='0'
    pattern ='DUNNOWHAT.COM'
    ignoreCase ='True'
    negate ='False'
}
}
Add-WebConfiguration @list

Tuesday, December 4, 2012 10:55 AM

Hello Blindrood, thanks again for the reply

i tried you code, it seems to work, because no errors or warnings are thrown, but unfortunately no conditions appear in the rule...Do they actually appear in your manager GUI?

Are you also on IIS7.5?

I think we're almost there..


Tuesday, December 4, 2012 12:34 PM

Alllright!! ran all the commands on a clean IIS and they affectively all worked including the condition !

Thank you!

i dit refresh the IIS manager though, ran a few more commands with the same content but no go...

maybe because of the different commands i used while yours is practically a one liner...

I'll verify some more if i should find the culprit i'll update this thread.

Many thanks for helping me get rid of this annoying issue!!


Monday, December 22, 2014 8:34 PM

Hello Koveral,

Could you please show me csv file through which your assigning your domain name


Tuesday, June 5, 2018 1:57 PM | 1 vote

I had researched this and the URLRewrite method works well.  Can't seem to find the change scripted anywhere well.  I wrote this compatible with PowerShell v2 and above and tested it on IIS 7.5.  

    # Add Allowed Server Variable
        Add-WebConfiguration /system.webServer/rewrite/allowedServerVariables -atIndex 0 -value @{name="RESPONSE_SERVER"}
    # Rule Name
        $ruleName = "Remove Server Response Header"
    # Add outbound IIS Rewrite Rule
        Add-WebConfigurationProperty -pspath "iis:\" -filter "system.webServer/rewrite/outboundrules" -name "." -value @{name=$ruleName; stopProcessing='False'}
    #Set Properties of newly created outbound rule 
        Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST"  -filter "system.webServer/rewrite/outboundRules/rule[@name='$ruleName']/match" -name "serverVariable" -value "RESPONSE_SERVER"
        Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST"  -filter "system.webServer/rewrite/outboundRules/rule[@name='$ruleName']/match" -name "pattern" -value ".*"
        Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST"  -filter "system.webServer/rewrite/outboundRules/rule[@name='$ruleName']/action" -name "type" -value "Rewrite"