Redirect your sites in SharePoint to a new site collection

Background

During a intranet migration there was a requirement to move the intranet from the root site to a site collection under "/sites". When moving site collections around in the SharePoint Admin Center a redirect will automatically be added to the old URL, so in many cases this is sufficient.

In this case we could use this approach, as you only can swap at not move away from the root site.

Solution

I modern sites the solution to add a redirect to the root site actually was much easier than one would anticipate.

  1. Go to the site you want to redirect away from, and from the front page select New and News link
  2. Add the full URL to the site and a title like Redirect
  3. Go to the Site Pages library at /SitesPages, ex. https://contoso.sharepoint.com/sites/news/SitePages
  4. Mark the newly created News link as the homepage of the site by selecting ... and Make homepage

When you now visit this site you will be redirected to the new site specified in the redirect link.

Tip: If you need to remove or change the redirect, add "?stay=true" to the end of the URL. Example: https://contoso.sharepoint.com/?stay=true

Redirect classic sites

If your migration also includes moving away from a classic publishing site to a modern communication site, you will face issues since the old URL pointed the users to the Pages/Default.aspx (or a similar folder if using other default languages).

A workaround could be to create a classic site pages library in the modern site named Pages and add a landing page informing the users of the new site address. Note that this only work on modern team sites, not for communication sites.

Using the PnP.PowerShell (https://pnp.github.io/powershell) module:

Connect-PnPOnline -Url https://contoso.sharepoint.com
New-PnPList -Title Pages -Template WebPageLibrary

Then go to the newly created library and add a page named Default. In this page you could add some information informing the end-users that the site has moved.

It is also possible to add a redirect using JavaScript like this example:

<script>
function corpRedirectPage() { 
	var urlParams = new URLSearchParams(window.location.search);
	var stay = urlParams.get('stay');
	
	if (stay === 'true') { 
		return; 
	}
	
	window.setTimeout(function() { 
		window.location.href = 'https://contoso.sharepoint.com/sites/news'; 
	}, 1500); 
} 

_spBodyOnLoadFunctionNames.push("corpRedirectPage");
</script>

If you need to remove or change the redirect, add "?stay=true" to the end of the URL. Example: https://contoso.sharepoint.com/Pages/Default.aspx?stay=true

Note: Using JavaScript is most likely blocked on the site due to the new default policies. This can be changed temporarily before adding the script to the page:

# Enable custom scripts
Set-PnPSite -Identity https://contoso.sharepoint.com -DenyAndAddCustomizePages:$false

# Add script to page

# Disable custom scripts
Set-PnPSite -Identity https://contoso.sharepoint.com -DenyAndAddCustomizePages:$true