Share via

Help with configuring Application Gateway Header Rewrite via Powershell

Pim Beliën 1 Reputation point
2021-03-02T15:47:24.387+00:00

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
Azure Application Gateway

An Azure service that provides a platform-managed, scalable, and highly available application delivery controller as a service.

0 comments No comments

1 answer

Sort by: Most helpful
  1. suvasara-MSFT 10,166 Reputation points Moderator
    2021-08-06T08:42:58.847+00:00

    @Pim Beliën , 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.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.