In this Article, we will look at a script that sets all websites to use non-www instead of www.
When I first got into designing websites, the long domain names and having WWW at the beginning meant you were a professional website.
Today, it is the shorter domain names and the non-www that make your website stand out.
This script will update your applicationHost.config file to work for all your websites. (Make a backup of your config before running this script, in case something goes wrong.)


I ran this on one of my [ARR Servers] ([Application Request Routing]), which is in a [Shared-Configuration] with several others.
Once you update one, all get updated.
Since the ARR is the [Front-End], and the Web Server is the [Back-End], we only need to apply this to our ARR Front-End servers.

[PowerShell - IIS set all websites to (non-www) instead of www.]
CFFCS | CarrzSynEdit: | PS (PowerShell)
$path = "G:\IIS_Config\applicationHost.config"
[xml]$xml = Get-Content $path

# 1. Target the globalRules section in system.webServer/rewrite

$rewriteNode = $xml.SelectSingleNode("//system.webServer/rewrite")
if ($null -eq $rewriteNode) {
    $rewriteNode = $xml.CreateElement("rewrite")
    $xml.SelectSingleNode("//system.webServer").AppendChild($rewriteNode) | Out-Null
}

$globalRulesNode = $rewriteNode.SelectSingleNode("globalRules")
if ($null -eq $globalRulesNode) {
    $globalRulesNode = $xml.CreateElement("globalRules")
    $rewriteNode.AppendChild($globalRulesNode) | Out-Null
}

# 2. Create the Non-WWW to WWW Redirect Rule

$rule = $xml.CreateElement("rule")
$rule.SetAttribute("name", "Global Non-WWW to WWW Redirect")
$rule.SetAttribute("stopProcessing", "true")

# Match anything

$match = $xml.CreateElement("match")
$match.SetAttribute("url", "(.*)")
$rule.AppendChild($match) | Out-Null

# Condition: Check if host does NOT start with www.

$conditions = $xml.CreateElement("conditions")
$add = $xml.CreateElement("add")
$add.SetAttribute("input", "{HTTP_HOST}")
$add.SetAttribute("pattern", "^www\.")
$add.SetAttribute("negate", "true")
$conditions.AppendChild($add) | Out-Null
$rule.AppendChild($conditions) | Out-Null

# Action: Redirect to https://www. + original host + path

$action = $xml.CreateElement("action")
$action.SetAttribute("type", "Redirect")
$action.SetAttribute("url", "https://www. {HTTP_HOST}/{R:1}") # Note: Space added per your constraint
$action.SetAttribute("redirectType", "Permanent")
$rule.AppendChild($action) | Out-Null

# 3. Add to Global Rules and Save

$globalRulesNode.AppendChild($rule) | Out-Null
$xml.Save($path)

Write-Host "Success: Global Non-WWW redirect added to all sites." -ForegroundColor Green
iisreset

For some reason, I received the following error, even though I am the Administrator.
Error
Restart attempt failed. Access denied, you must be an administrator of the remote computer to use this
command. Either have your account added to the administrator local group of
the remote computer or to the domain administrator global group.
Running this resolved the iisreset issue.
[PowerShell - iisreset]
CFFCS | CarrzSynEdit: | PS (PowerShell)
Stop-Service WAS -Force
Start-Service W3SVC

Other Articles Related to this Entry.