We are migrating from SP 2013 to SP 2016. There are multiple sub-sites under the main site and further sub-sites under those sub-sites. The users may have bookmarked specific libraries and lists from the old URL and they need to be routed to the main page of the sites with the new URL. For example,





Now the sub-sites in the SP 2013 are inheriting the master page from the Subsite1 level. If it was a simple redirect where every site had their own master page I could have used JavaScript like:

location.href="[new url]"

and be done with it. Now I believe I need if-else statements in the sub-site 1 master page to check for what URL the user is using and then redirect it.

I have tried the following:


else if(location.href=='https://abc/subsite1/subsite2/subsite3/')

The issue is user may have bookmarked a list from abc and the check would fail. How do I just check for the path till sub-site2 with JavaScript?

I am open to other ways of achieving this in SharePoint as well.

3 Answers 3


You could split the path to an array so you could check the sub site url(path).

var pathArray = window.location.pathname.split('/');
var Location = pathArray[index];

I would recommend you handle this at IIS level rather than implement something in masterpage. You can implement rule based redirect using URL rewrite module, and supports complex pattern matching (regex). As you migrate content to new environment, you can keep adding new rules based on your new information architecture.


To help the people facing the same problem, this is how i solved the issue:

Since I wanted to check for the sub-sites in the URL and redirect accordingly. I used the following JavaScript code:

        location.href='<URL where you want to route>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.