question

PimBelien avatar image
0 Votes"
PimBelien asked suvasara-MSFT answered

Help with configuring Application Gateway Header Rewrite via Powershell

Hi all,

I am stuck with trying to create a header rewrite rule for my application gateway. Unfortunately I am bound to certain policies so I only do this via Azure Devops and some Powershell or CLI scripts. Lucky for me the documentation provides a perfect example.
Also unfortunately I cannot seem to make it work even though everything seems to be configured correctly.

So I basically copied the script from the documentation and replace the regex expressions:

 $responseHeaderConfiguration = New-AzApplicationGatewayRewriteRuleHeaderConfiguration -HeaderName "Location" -HeaderValue "https://gwhostname.com{http_resp_Location_2}"
    
 $actionSet = New-AzApplicationGatewayRewriteRuleActionSet -ResponseHeaderConfiguration $responseHeaderConfiguration
     
 $condition = New-AzApplicationGatewayRewriteRuleCondition -Variable "http_resp_Location" -Pattern "(http?):\/\/.*azurewebsites\.net(.*)$" -IgnoreCase
     
 $rewriteRule = New-AzApplicationGatewayRewriteRule -Name LocationHeader -ActionSet $actionSet -Condition $condition
     
 $rewriteRuleSet = New-AzApplicationGatewayRewriteRuleSet -Name LocationHeaderRewrite -RewriteRule $rewriteRule
     
 $appgw = Get-AzApplicationGateway -Name $(GatewayName) -ResourceGroupName $(ResourceGroupName)
     
 $reqRoutingRule = Get-AzApplicationGatewayRequestRoutingRule -Name $(GatewayRule) -ApplicationGateway $appgw
     
 Add-AzApplicationGatewayRewriteRuleSet -ApplicationGateway $appgw -Name LocationHeaderRewrite -RewriteRule $rewriteRuleSet.RewriteRules
     
 Set-AzApplicationGatewayRequestRoutingRule -ApplicationGateway $appgw -Name $(GatewayRule) -RuleType $reqRoutingRule.RuleType -HttpListenerId $reqRoutingRule.HttpListener.Id -RewriteRuleSetId $rewriteRuleSet.Id -UrlPathMapId $reqRoutingRule.UrlPathMap.Id
     
 Set-AzApplicationGateway -ApplicationGateway $appgw


The commands were executed and I can see the configurations in the portal. However when verifying the Location header does not get rewritten. I even tried removing the condition, and providing an always true condition but a rewrite never happend.

Also in the Rewrites blade I see the Rules applied set to 1. However when going to the Update rewrite set blade, suddenly it doesn't seem to be associated to any routing rule (none of the boxes are checked). I noticed that when configuring everything manually in my own Azure portal the boxes of the latter are checked when applying the rewrite rule.

To me it seems that the Powershell script is missing something, but I am unable to debug any further. Does anyone faced this issue before? Or can help me find out how to make this work?

azure-application-gateway
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

suvasara-MSFT avatar image
0 Votes"
suvasara-MSFT answered

@PimBelien, the rewrite configuration is attached to the source listener via the routing rule. When you use a basic routing rule, the header rewrite configuration is associated with a source listener and is a global header rewrite. When you use a path-based routing rule, the header rewrite configuration is defined on the URL path map. In that case, it applies only to the specific path area of a site.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.