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:
Supported by Microsoft: Source
HTTP REDIRECT
Client makes a request to http://www.contoso.com/sites/site1. IIS with ARR receives the request to http://www.contoso.com/sites/site1. ARR uses URL Rewrite to create a redirect to http://www.fabrikam.com/sites/site1, and then returns the HTTP redirect (for example, 301 or 302) to the client Client receives the redirect, and then makes a new request by using the new URL: http://www.fabrikam.com/sites/site1. SharePoint receives the request, and processes it as usual.
This scenario is SUPPORTED.
Fast solution with client site permanent redirect
Tested solution
Complex regex rules possible
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:
Title | Regex | Description |
---|---|---|
DefaultMySite | ^default.aspx | About me |
PersonMySite | person.aspx | Activities |
MyPeopleMySite | mypeople.aspx | Newsfeed entries |
Documents | personal/\w*/_layouts/15/onedrive.aspx | Onedrive documents |
Blog | personal/\w*/Blog/* | Personal blog |
Personal_Default | personal/\w*/default.aspx | Personal newsfeed |
AddApp | personal/\w*/_layouts/15/addanapp.aspx | Add apps to mysite |
Newsbweb | personal/\w*/_layouts/15/newsbweb.aspx | Create new subsites |
Sitemanager | personal/\w*/_layouts/15/sitemanager.aspx | Site Manager |
SocialSites | personal/\w*/Social/Sites.aspx | Followed sites |
FollowedDocumentsSites | personal/\w*/Social/FollowedContent.aspx | Followed documents |
SharePointHome | _layouts/15/sharepoint.aspx | Followed site activities |
Quicklinks | _layouts/15/MyQuickLinks.aspx | Quick links |
HashTags | _layouts/15/HashTagProfile.aspx | Followed 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