Mathias Osterkamp

Specialist – focus development Microsoft technology stack

Mysite - Redirects

Redirect Mysite to a modern Page

Redirect Mysite to a modern Page

Goal

SharePoint 2019 OnPremise has still the classic MySite. To support a modern page you could build it on top of the default MySite host, but this is not the best way. With a redirect based solution you don’t change anything to existing code. Just build your new MySite features on a new page on a different WebApplication and SiteCollection based on modern theme. All important links should be redirected.

Possible Solutions

To get your MySite pages redirected you face one problem. It is not only the default.aspx on MySite Host, there are several more pages especial on the user generated personal site collections. (ex. https://mysite.contoso.com/personal/«loginname»/…)

IIS Redirect

Best solution is to use the features of IIS URL Rewrite Module. It has to be installed first on every SharePoint frontend server:

URL Rewrite Module 2.1: https://www.iis.net/downloads/microsoft/url-rewrite

Advantages:

Disadvantages:

  • No permission based redirect, you had to implement a permission based logic on target page

HTTP Handler

With a Http Handler you can access every request in IIS to add some custom logic.(Sample)

Code from stack overflow to demonstrate the basic solution link :

<add name="CustomHttpModule" type="CustomHttpModule.HttpModuleImplementation, CustomHttpModule" />
using System;
using System.Web;
using System.Web.UI;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;

namespace CustomHttpModule
{
    public class HttpModuleImplementation : IHttpModule
    {
        #region IHttpModule Members

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            if (context == null)
                throw new ArgumentNullException("Context == null");

            context.AuthorizeRequest += new EventHandler(this.ProcessRequestHandler);
        }
        #endregion

        private void DummpRequest(object sender, EventArgs e)
        {
        }
        //first check that user.identity record exist in database
        //If not then forward user to User registration page
        private void ProcessRequestHandler(object sender, EventArgs e)
        {
            try
            {
                HttpApplication context = (HttpApplication)sender;
                string strAbsoluteUri = context.Request.Url.AbsoluteUri.ToLower();
                //check if request is accessing aspx page
                if (strAbsoluteUri.Substring(strAbsoluteUri.Length - 5, 5).Contains(".aspx"))
                {
                    string userName = context.User.Identity.Name;
                    //replace Test Module with DB call to validate user data
                    if (!CheckUserInDb(userName))
                    {
                        if (!strAbsoluteUri.Contains("mypage.aspx"))
                            redirectToRegistrationPage(context);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        private void redirectToRegistrationPage(HttpApplication context)
        {
            context.Response.Redirect("http://" + context.Request.ServerVariables["HTTP_HOST"].ToString() + "Regpage.aspx", false);
        }

        private bool CheckUserInDb(string userName)
        {
                    return true;
        }
    }
}

Advantages:

  • Support for complex rules and permission based redirects

Disadvantages:

  • SharePoint Solution needed
  • A lot of work to make good implementation
  • Rules have to be stored
  • Untested
  • Slow

Modify Pages

You also can modify each target page to add some redirect logic for example:

<meta http-equiv="refresh" content="0;url=https://sharepoint.contoso.com/Sites/Home.aspx" />

Advantages:

  • Modify only specific pages

Disadvantages:

  • You can not modify system pages like “Edit Profile”
  • No easy way to redirect personal pages

Pages for consideration

Following pages should be under consideration:

TitleRegexDescription
DefaultMySite^default.aspxAbout me
PersonMySiteperson.aspxActivities
MyPeopleMySitemypeople.aspxNewsfeed entries
Documentspersonal/\w*/_layouts/15/onedrive.aspxOnedrive documents
Blogpersonal/\w*/Blog/*Personal blog
Personal_Defaultpersonal/\w*/default.aspxPersonal newsfeed
AddApppersonal/\w*/_layouts/15/addanapp.aspxAdd apps to mysite
Newsbwebpersonal/\w*/_layouts/15/newsbweb.aspxCreate new subsites
Sitemanagerpersonal/\w*/_layouts/15/sitemanager.aspxSite Manager
SocialSitespersonal/\w*/Social/Sites.aspxFollowed sites
FollowedDocumentsSitespersonal/\w*/Social/FollowedContent.aspxFollowed documents
SharePointHome_layouts/15/sharepoint.aspxFollowed site activities
Quicklinks_layouts/15/MyQuickLinks.aspxQuick links
HashTags_layouts/15/HashTagProfile.aspxFollowed HashTag Newsfeeds

IIS Redirect Rules

What also should work is to redirect other accounts. So we still keep url parameters (default for iis rules):

…/person.aspx?accountname=«loginname» > …/Sites/Home.aspx?accountname=«loginname»

You can also create a url parameter to stop redirect, if you like still have an access possibility. For example https://mysite.contoso.com/default.aspx?stopredirect=1 :

<conditions>
    <add input="{QUERY_STRING}" pattern="stopredirect=1" negate="true" />
</conditions>

Here is a list of important rules:

<rule name="Redirect_Default_MySite" stopProcessing="true">
    <match url="^default\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent" />
</rule>
<rule name="Redirect_Person_MySite" stopProcessing="true">
    <match url="person\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent" />
</rule>
<rule name="Redirect_MyPeople_MySite" stopProcessing="true">
    <match url="mypeople\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent" />
</rule>
<rule name="Redirect_OneDrive" stopProcessing="true">
    <match url="personal\/\w*\/_layouts/15/onedrive\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent" />
</rule>
<rule name="Redirect_Blog" stopProcessing="true">
    <match url="personal\/\w*\/Blog\/\*" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent"/>
</rule>
<rule name="Redirect_Personal_Default" stopProcessing="true">
    <match url="personal\/\w*\/default\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent"/>
</rule>
<rule name="Redirect_AddApp" stopProcessing="true">
    <match url="personal\/\w*\/_layouts/15/addanapp\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent"/>
</rule>
<rule name="Redirect_Newsbweb" stopProcessing="true">
    <match url="personal\/\w*\/_layouts/15/newsbweb\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent"/>
</rule>
<rule name="Blocken_Sitemanager" stopProcessing="true">
    <match url="personal\/\w*\/_layouts/15/sitemanager\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent"/>
</rule>
<rule name="Redirect_SocialSites" stopProcessing="true">
    <match url="personal\/\w*\/Social/Sites\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent" />
</rule>
<rule name="Redirect_FollowedDocumentSites" stopProcessing="true">
    <match url="personal\/\w*\/Social/FollowedContent\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent" />
</rule>
<rule name="Redirect_SharePointHome" stopProcessing="true">
    <match url="_layouts/15/sharepoint\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent" />
</rule>
<rule name="Redirect_Quicklinks" stopProcessing="true">
    <match url="_layouts/15/MyQuickLinks\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent" />
</rule>
<rule name="Redirect_HashTags" stopProcessing="true">
    <match url="_layouts/15/HashTagProfile\.aspx" />
    <action type="Redirect" url="https://sharepoint.contoso.com/Sites/Home.aspx" redirectType="Permanent" />
</rule>

More documentation from Microsoft here: https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module