Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.
Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.

How to point to a custom 404 error Web page

Recently I was working on a custom 404 page with JavaScript logic for retiring an old SharePoint farm.   Replacing the “oldportal” DNS name with “newportal” helped avoid any end user 404 message by automatically handling all redirects.   Microsoft has a great KB article with implementation steps.  http://support.microsoft.com/kb/941329   However, I had two important changes and wanted to blog about it in case this might help others.

  1. Custom JavaScript redirect logic.   A simple replace statement helps change “oldportal” to “newportal” and seamlessly transition user traffic to the new system.
  2. PowerShell instead of Visual Studio EXE.   Not everyone has Visual Studio or the developer skills needed for compiling a command line EXE.   PowerShell is a simple text command you can run on MOSS2007/SP2010/SP2013 because it references the “Microsoft.SharePoint” assembly and works on older SharePoint server versions.

Enjoy! 

Smile

 

PowerShell Code – apply web application setting

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup("http://oldportal")
$webapp.FileNotFoundPage = "Custom404.htm"
$webapp.Update()

 

HTML Code – Custom404.htm

<!-- _localBinding -->
<!-- _lcid="1033" _version="" -->
<html>
<head>
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=utf-8" />
<meta HTTP-EQUIV="Expires" content="0" />
<noscript>
	<meta http-equiv="refresh" content="0; url=/_layouts/spsredirect.aspx?noscript=1" />
</noscript>
<script language='javascript' src="_/layouts/lO33/init.js"></script>
<script language='javascript' src="_/layouts/1033/core.js"></script>
<script language='javascript'>
//requestedUrl = escapeProperly(window.location.href);
//STSNavigate(”/layouts/.?oldUrl=” + requestedUrl);
var dest = window.location.href.replace("oldportal","newportal") .toString();
window.location.href = dest;
</script>
</head>
<body>
</body>
</html>

© Copyright 2016
@ SPJeff

Return to Top ▲Return to Top ▲