Introduction
Azure Application Gateway URL rewrite rules modify request URLs and headers before forwarding to backends. When rewrite rules don't work as expected, requests reach backends with incorrect paths or headers.
Symptoms
Rewrite not applied:
```bash $ curl -I https://myapp.example.com/api/users
# Expected: Request routed to /v2/api/users # Actual: Request reaches backend as /api/users ```
Rule condition not matching:
# Rewrite rule configured but URL unchanged
# Backend receives original path, not rewritten pathHeader not modified:
```bash $ curl -I https://myapp.example.com/api/users
# X-Custom-Header should be added but missing ```
Common Causes
- 1.Rule not attached to listener - Rewrite set not associated
- 2.Condition not matching - Pattern doesn't match request
- 3.Rule priority wrong - Other rule executes first
- 4.Wrong request stage - Rewrite at wrong processing stage
- 5.Backend path override - Backend pool path override conflicts
- 6.Case sensitivity - Pattern matching case issue
- 7.Rule disabled - Rewrite rule set not enabled
Step-by-Step Fix
Step 1: Check Rewrite Rule Set Association
```bash # List rewrite rule sets az network application-gateway rewrite-rule set list \ --gateway-name my-appgw \ --resource-group my-rg \ --query '[].{Name:name,Rules:rewriteRules}'
# Check if rewrite set attached to routing rule az network application-gateway rule show \ --gateway-name my-appgw \ --name my-rule \ --resource-group my-rg \ --query '{Name:name,RewriteSet:rewriteRuleSet}'
# Attach rewrite set to routing rule az network application-gateway rule update \ --gateway-name my-appgw \ --name my-rule \ --resource-group my-rg \ --rewrite-rule-set my-rewrite-set ```
Step 2: Check Rewrite Rule Conditions
```bash # Show rewrite rule configuration az network application-gateway rewrite-rule show \ --gateway-name my-appgw \ --rule-set-name my-rewrite-set \ --name my-rewrite-rule \ --resource-group my-rg \ --query '{Conditions:conditions,Actions:requestHeaderConfigurations,urlConfigurations}'
# Common condition patterns: # - Request URL path pattern # - Query string parameter # - Request header value # - Response status code
# Update condition pattern az network application-gateway rewrite-rule update \ --gateway-name my-appgw \ --rule-set-name my-rewrite-set \ --name my-rewrite-rule \ --resource-group my-rg \ --conditions '[{"variable":"var_request_uri","pattern":"^/api/","ignoreCase":true}]' ```
Step 3: Check URL Rewrite Action
```bash # Check URL rewrite configuration az network application-gateway rewrite-rule show \ --gateway-name my-appgw \ --rule-set-name my-rewrite-set \ --name my-rewrite-rule \ --resource-group my-rg \ --query 'urlConfigurations'
# URL rewrite options: # - modifiedPath: Replace path # - modifiedQueryString: Replace query string # - reroute: True to re-evaluate routing
# Update URL rewrite action az network application-gateway rewrite-rule update \ --gateway-name my-appgw \ --rule-set-name my-rewrite-set \ --name my-rewrite-rule \ --resource-group my-rg \ --url-configuration '{"modifiedPath":"/v2{path}","reroute":true}'
# Path patterns: # {path} - original path # {query_string} - original query string # {host} - original host ```
Step 4: Check Rule Priority and Sequence
```bash # List rewrite rules in set az network application-gateway rewrite-rule list \ --gateway-name my-appgw \ --rule-set-name my-rewrite-set \ --resource-group my-rg \ --query '[].{Name:name,Sequence:ruleSequence}'
# Rules execute in sequence order # Update sequence if needed az network application-gateway rewrite-rule update \ --gateway-name my-appgw \ --rule-set-name my-rewrite-set \ --name my-rewrite-rule \ --resource-group my-rg \ --rule-sequence 100
# Lower sequence executes first ```
Step 5: Check Request Stage
```bash # Rewrite rules can execute at different stages: # - Request routing stage (before backend selection) # - Response stage (before returning to client)
# For URL rewriting, use Request routing stage # Check rewrite rule set type az network application-gateway rewrite-rule set show \ --gateway-name my-appgw \ --name my-rewrite-set \ --resource-group my-rg \ --query '{Name:name,Rules:rewriteRules}' ```
Step 6: Check for Path-Based Rule Conflicts
```bash # Path-based rules may conflict with rewrite az network application-gateway url-path-map show \ --gateway-name my-appgw \ --name my-path-map \ --resource-group my-rg \ --query '{PathRules:pathRules,Default:defaultBackendAddressPool}'
# Path rules: # /api/* -> backend-api # /web/* -> backend-web
# If rewrite changes /api to /web, path rule may re-route # Use reroute: true to re-evaluate after rewrite ```
Step 7: Test Condition Matching
```bash # Enable Application Gateway logs az monitor diagnostic-settings create \ --name appgw-logs \ --resource /subscriptions/SUB/resourceGroups/my-rg/providers/Microsoft.Network/applicationGateways/my-appgw \ --workspace /subscriptions/SUB/resourcegroups/my-rg/providers/microsoft.operationalinsights/workspaces/my-workspace \ --logs '[{"category":"ApplicationGatewayAccessLog","enabled":true},{"category":"ApplicationGatewayFirewallLog","enabled":true}]'
Step 8: Debug with Custom Headers
```bash # Add debug header to verify rewrite executed az network application-gateway rewrite-rule update \ --gateway-name my-appgw \ --rule-set-name my-rewrite-set \ --name my-rewrite-rule \ --resource-group my-rg \ --add-request-header '[{"headerName":"X-Rewrite-Debug","headerValue":"executed"}]'
# Test with curl curl -I https://myapp.example.com/api/users
# Check for X-Rewrite-Debug header in response # If present, rewrite rule executed # If not, condition didn't match or rule not attached ```
Step 9: Check Backend Path Settings
```bash # Backend HTTP settings may override path az network application-gateway http-settings show \ --gateway-name my-appgw \ --name my-http-settings \ --resource-group my-rg \ --query '{Path:path,HostName:hostName,PickHostName:pickHostNameFromBackendAddress}'
# If backend path set, it prepends to request path # This may conflict with rewrite
# Remove backend path override if needed az network application-gateway http-settings update \ --gateway-name my-appgw \ --name my-http-settings \ --resource-group my-rg \ --path "" ```
Step 10: Create New Rewrite Rule
```bash # Create new rewrite rule set az network application-gateway rewrite-rule set create \ --gateway-name my-appgw \ --name new-rewrite-set \ --resource-group my-rg
# Create rewrite rule az network application-gateway rewrite-rule create \ --gateway-name my-appgw \ --rule-set-name new-rewrite-set \ --name api-version-rewrite \ --resource-group my-rg \ --sequence 100 \ --conditions '[{"variable":"var_request_uri","pattern":"^/api/","ignoreCase":true}]' \ --url-configuration '{"modifiedPath":"/v2{path}","reroute":true}'
# Attach to routing rule az network application-gateway rule update \ --gateway-name my-appgw \ --name my-rule \ --resource-group my-rg \ --rewrite-rule-set new-rewrite-set ```
Rewrite Rule Variables
| Variable | Description | Example |
|---|---|---|
| var_request_uri | Full request URI | /api/users?id=1 |
| var_request_path | Path only | /api/users |
| var_query_string | Query string | id=1 |
| var_host_name | Host header | myapp.example.com |
| var_request_method | HTTP method | GET, POST |
| var_http_{header} | Request header | var_http_content_type |
Verification
```bash # After fixing rewrite rule # Test URL rewrite curl -v https://myapp.example.com/api/users
# Expected: # Request path: /api/users # Backend receives: /v2/api/users
# Verify request routed to correct backend ```
Prevention
To prevent Azure Application Gateway URL path rewrite issues from recurring, implement these proactive measures:
1. Monitor Rewrite Rule Execution
groups:
- name: azure-appgateway
rules:
- alert: AzureAppGatewayRewriteFailures
expr: |
rate(azure_appgateway_rewrite_failures_total[5m]) > 0
for: 5m
labels:
severity: warning
annotations:
summary: "Azure Application Gateway rewrite rule failures detected"2. Test Rewrite Rules Before Production
```bash # Create test script for rewrite rules cat << 'EOF' > test_rewrite.sh #!/bin/bash TEST_URLS=( "https://myapp.example.com/api/users" "https://myapp.example.com/api/v1/products" "https://myapp.example.com/legacy/orders" )
for url in "${TEST_URLS[@]}"; do echo "Testing: $url" RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$url") if [ "$RESPONSE" == "200" ] || [ "$RESPONSE" == "401" ]; then echo "PASS: Got $RESPONSE" else echo "FAIL: Got $RESPONSE" fi done EOF
chmod +x test_rewrite.sh ```
3. Document Rewrite Rules
```bash # Export current rewrite configuration az network application-gateway rewrite-rule set show \ --gateway-name my-appgw \ --resource-group my-rg \ --rule-set-name my-ruleset \ --name my-rule \ -o yaml > rewrite_config.yaml
# Store in version control git add rewrite_config.yaml git commit -m "Document rewrite rules" ```
Best Practices Checklist
- [ ] Monitor rewrite rule execution
- [ ] Test rewrite rules before production
- [ ] Document rewrite configuration
- [ ] Use consistent naming conventions
- [ ] Test backend routing
- [ ] Review logs for failures
Related Issues
- [Fix Azure Application Gateway Redirect Loop](/articles/fix-azure-redirection-rule-loop)
- [Fix Azure Application Gateway WAF Blocking](/articles/fix-azure-application-gateway-waf-blocking)
- [Fix Azure Backend Pool Not Resolving](/articles/fix-azure-backend-pool-not-resolving)
Related Articles
- [Technical troubleshooting: Fix Azure Aks Pod Crashloopbackoff Issue in Azure](azure-aks-pod-crashloopbackoff)
- [Technical troubleshooting: Fix Azure Api Management Policy Expression Runtime](azure-api-management-policy-expression-runtime-error)
- [Technical troubleshooting: Fix Azure App Configuration Feature Flag Not Refre](azure-app-configuration-feature-flag-not-refreshing)
- [Technical troubleshooting: Fix Azure App Service 503 Always On Disabled Issue](azure-app-service-503-always-on-disabled)
- [Technical troubleshooting: Fix Azure Application Gateway Err SSL Unrecognized](azure-application-gateway-err-ssl-unrecognized-name-alert)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Fix Azure Application Gateway URL Path Rewrite Not Working", "description": "Troubleshoot Azure Application Gateway rewrite rule issues. Check rule configuration, condition matching, and header settings.", "url": "https://www.fixwikihub.com/fix-azure-url-path-rewrite-not-working", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2026-04-03T13:47:17.295Z", "dateModified": "2026-04-03T13:47:17.295Z" } </script>