Although there are many methods to overcome this problem, such as the site backup/restore method described by Todd Klindt, these methods become increasingly more time consuming as the site size grows. The easiest method in my mind still remains using the SPSite.Rename() method. Unfortunately, the MSDN article states at the top that it "Changes the URL of a host-header-named site collection to a new URL.". I believe this is why this method has remained obscure and under-leveraged. At the end of the article, it also states "After the site rename, an app pool recycle is recommended to force refreshing the cache." which is incorrect. An app pool recycle is required in order for the new URL to work correctly.
But enough picking apart MSDN articles... let's get to the good stuff. :-)
The following Powershell script does what we want:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
Param ([Parameter(Mandatory=$true, HelpMessage="Current site URL?")][ValidateNotNullOrEmpty()][string]$oldUrl, | |
[Parameter(Mandatory=$true, HelpMessage="Desired new site URL?")][ValidateNotNullOrEmpty()][string]$newUrl) | |
$validate = Get-SPSite $newUrl; | |
if ($validate -eq $null) | |
{ | |
try | |
{ | |
$site = Get-SPSite $oldUrl; | |
$site.Rename($newUrl); | |
Write-Host "$oldUrl successfully changed to $newUrl" -ForgroundColor Green; | |
} | |
catch | |
{ | |
Write-Host -ForegroundColor Red $_.Exception.Message; | |
} | |
} | |
else | |
{ | |
Write-Host "ERROR! The URL ($newUrl) already exists as a site. Action cancelled." -ForegroundColor Red; | |
} |
The core of the script is lines 9 and 10.
Remember to recycle your web app's app pool!
Enjoy!
C