Reporting and deleting orphaned SharePoint redirect sites
There are a few new options in SharePoint Online. You now can change the URL of the site, change the root site and swap them with another. To support these scenarios the site redirect functionality is introduced. This means a new redirect is created each time the action occurs. A redirect ensures that links pointing to the ‘old’ URL will continue to work. And users can keep working even though they might have old bookmarks.
Redirects
If you would change a URL the old site will be replaced. The replacement site uses a different template: REDIRECTSITE#0. It contains all the logic to redirect the browser request to the new site. To redirect it uses the 308 redirect status.
The admin center
Reading the documentation to manage site redirects will explain to you that to remove the old site you should use PowerShell. If you delete a renamed or swapped site using the Admin Center, the redirect stays present in your tenant. Depending on the size of your tenant you could potentially end up with several orphaned redirect sites. While they are not visible they are present and keep redirecting users. So as soon as you delete the target location users will be presented a nice 404 file not found page.
In our case, we had a few renamed sites that were deleted since they were obsolete. The admin in the case had deleted them through the new admin center. Where there is no visual identification of the redirect sites. And thus users kept getting their redirects. Ending up at locations no longer in existence.
PowerShell to the rescue
As you can use PowerShell to query for all redirect sites. That information can then be used to see if the target location is still present. If the site has redirects and redirects to a page to a 404 you can either report the site. Or you can clean up and delete the site. A simple script can thus help you in the visibility of orphaned redirects.
To achieve this all you need is the Invoke-WebRequest
method. The method allows reading both the headers and status codes from requests. To prevent issues with redirects you want to use the -MaximumRedirection
flag when executing.
A simple script results in the following:
Connect-SPOService -Url https://tenant-admin.sharepoint.com
Get-SPOSite -Template REDIRECTSITE#0 | ForEach-Object {
$redirectSite = Invoke-WebRequest -Uri $_.Url -MaximumRedirection 0
$body = $null
Write-Host -f Green "Checking old URL for redirect" $_.Url
if($redirectSite.StatusCode -eq 308) {
Try {
Write-Host -f Green " Redirects to: " $redirectSite.Headers.Location
$body = Invoke-WebRequest -Uri $redirectSite.Headers.Location -MaximumRedirection 0 -ErrorAction SilentlyContinue
}
Catch{
if($_.Exception.Response.StatusCode -eq "NotFound") {
Write-Host -f Red " Target location no longer exists, should be removed"
Remove-SPOSite -Identity $redirectSite.Headers.Location -confirm:$false
}
}
Finally {
If($body.StatusCode -eq "302"){
Write-host -f Yellow " Target location still exists"
}
}
}
}