You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于Azure应用网关外部站点重定向配置的PowerShell脚本咨询

Analysis & Improvements for Your Azure Application Gateway Redirect Script

First, let's break down the gaps in your current script and walk through a fully functional, updated version that works reliably.

Key Correctness Issues in Your Current Script

  • Outdated Azure Module: You’re using the deprecated AzureRm cmdlets. Microsoft has fully moved to the Az module for all Azure PowerShell operations, and AzureRm no longer receives updates or support—this will cause errors in newer Azure environments.
  • Incomplete Redirect Configuration: Your Add-AzureRmApplicationGatewayRedirectConfiguration line is truncated. It’s missing mandatory parameters like -ApplicationGateway, -RedirectType, and -TargetUrl—without these, the command can’t create a valid redirect setup.
  • Unlinked Redirect to Rule: You specified an existing rule name, but there’s no step to connect the new redirect configuration to this rule. The rule won’t actually use the redirect until you explicitly associate them.
  • Typo in Listener Variable: $listnerName has a typo (missing an "e"—should be $listenerName). This will break the script when it tries to reference your listener.

Fully Improved & Functional Script

Here’s a polished version using the modern Az module, with all missing steps and best practices included:

# Configuration Variables - Update these to match your environment
$AppgwName = "appgwname"
$rgName = "rgname"
$ExternalSite = "http://www.google.com"
$RedirectConfigurationName = "ExternalSiteRedirect"
$ListenerName = "your-listener-name"  # Fixed typo here
$ExistingRuleName = "your-existing-rule-name"

# 1. Retrieve the target Application Gateway object
$gw = Get-AzApplicationGateway -Name $AppgwName -ResourceGroupName $rgName

# 2. Add the redirect configuration to the gateway
$gw = Add-AzApplicationGatewayRedirectConfiguration -ApplicationGateway $gw `
    -Name $RedirectConfigurationName `
    -RedirectType Found `  # Use "Permanent" for 301 (SEO-friendly) or "Found" for 302 (temporary) redirects
    -TargetUrl $ExternalSite `
    -IncludePath $false `  # Set to $true if you want to pass the original request path to the external site
    -IncludeQueryString $false  # Set to $true if you want to forward query strings

# 3. Link the redirect configuration to your existing routing rule
$existingRule = Get-AzApplicationGatewayRequestRoutingRule -ApplicationGateway $gw -Name $ExistingRuleName
$gw = Set-AzApplicationGatewayRequestRoutingRule -ApplicationGateway $gw `
    -Name $ExistingRuleName `
    -RuleType Basic `  # Use "PathBasedRouting" if your rule uses path maps
    -Listener $existingRule.Listener `
    -RedirectConfiguration $gw.RedirectConfigurations[$RedirectConfigurationName]

# 4. Commit all changes to the Application Gateway (critical step!)
Set-AzApplicationGateway -ApplicationGateway $gw

Additional Production-Grade Tips

  • Redirect Type: Pick Permanent (301) for permanent URL changes (helps with SEO) or Found (302) for temporary redirects.
  • HTTPS Compatibility: If your listener uses HTTPS, ensure the external site URL uses https:// to maintain secure connections.
  • Error Handling: For production scripts, add try/catch blocks to catch issues like missing gateways, listeners, or rules, and include verbose logging for debugging.
  • Path/Query Control: Adjust -IncludePath and -IncludeQueryString based on whether you want to pass the original request’s path or query parameters to the external site.

内容的提问来源于stack exchange,提问作者Eldos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:41:57