The following script is for the node.js platform and, if run on a server with your old host name's resolution, will intercept incoming http(s) requests, map it to your new server, allowing for site renaming and provide the user with a message as well as the new 'best guess' url to what they were trying to reach. Created to address the need to rename a SharePoint farm in an upgrade and help deal with the resulting broken links.
// Kevin Guyer - 2013
// Node.js script to handle server (SharePoint) name changes
// This is designed to catch the use of old urls and present an infomational page with the new url to the resource
// Test with this to see URL altering: http://localhost:8080/hr/foosite/barsite/somelibrary/somedoc.pdf
// Intercept incoming port 80 or 443 (needs to operate on both) - set the port below for flavor you are running
var http = require('http');
var url = require("url");
var server = http.createServer();
server.on('request', function(request, response) {
var hostname = request.headers.host;
var newHostname = 'https://yournewfarmpath'; // no trailing slash, set to your new farm/server name and path
var pathname = url.parse(request.url).pathname;
var newPathname = pathname;
var query = url.parse(request.url).query; // optional, not used yet
// Run match logic on array elements to determine what needs to be displayed
var aResPath = pathname.split('/');
// Modify path elements as needed with element matching:
var wasAltered = false;
if(aResPath.length >=1){
try{
if(aResPath[1].toLowerCase() == 'hr'){ aResPath[1] = 'HumanResources'; wasAltered = true; }
if(aResPath[3].toLowerCase() == 'barsite'){ aResPath[3] = 'NewBarSite'; wasAltered = true; }
// add others as needed, sites on root starting at index [1]...
}
catch(err) {
// handle as desired...
}
if(wasAltered == true){
console.log("Detected swap string in path, altering output.");
newPathname = '';
for(i = 1; i < aResPath.length; i++){
newPathname = newPathname + '/' + aResPath[i];
}
}
}
// Build response message
var payload = '<h2>Oops.</h2><p>The url you attempted to reach (<em>' + hostname + pathname + '</em>) is no longer valid.</p>';
// Optional: Build and supply a best bet url
payload += '<p>Our best guess for the new url for this resource is <a href="'+ newHostname + newPathname+'">'+ newHostname + newPathname+'</a>.</p>';
payload += '<p>Please update your favorite or link source to reflect this new address.</p>';
// Optional step to log the old url hit via a web API (REST) - todo...
// Build and deliver response
response.writeHead(200, {"Content-Type": "text/html"});
response.write(payload);
response.end();
});
var port = 8080; // change this to 80 or 443 as needed
server.listen(port);
server.once('listening', function() {
console.log('Redirect server is now listening on port %d', port);
});