Skip to content

Instantly share code, notes, and snippets.

@dagstuan
Created May 20, 2015 12:56
Show Gist options
  • Save dagstuan/36de09485ea0080ce707 to your computer and use it in GitHub Desktop.
Save dagstuan/36de09485ea0080ce707 to your computer and use it in GitHub Desktop.
Legacy redirector for episerver CMS 5 and 6 urls
using System.Text.RegularExpressions;
using System.Web;
using EPiServer.Core;
using EPiServer.ServiceLocation;
using EPiServer.Web.Routing;
namespace Sintef.Common.Util
{
public class LegacyRedirector
{
public static int GetIdForLegacyUrl(string url)
{
var epi5Regex = new Regex(@"(content/\w+|default)____(?<id>\d+)\.aspx", RegexOptions.IgnoreCase);
var epi5Match = epi5Regex.Match(url);
var contentId = 0;
if (epi5Match.Success)
{
contentId = int.Parse(epi5Match.Groups["id"].Value);
}
else
{
var epi6Regex = new Regex(@"content/.+\.aspx\?(.*&)*id=(?<id>\d+)", RegexOptions.IgnoreCase);
var epi6Match = epi6Regex.Match(url);
if (epi6Match.Success)
{
contentId = int.Parse(epi6Match.Groups["id"].Value);
}
}
return contentId;
}
public static void TryRedirect(HttpRequest request, HttpResponse response, HttpContext context)
{
var fullOrigionalpath = request.Url.ToString();
var contentId = GetIdForLegacyUrl(fullOrigionalpath);
if (contentId != 0)
{
var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();
var contentReference = new ContentReference(contentId);
var link = urlResolver.GetUrl(contentReference);
if (!string.IsNullOrEmpty(link))
{
response.RedirectPermanent(link);
context.RewritePath(link);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment