Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xumix/0fad01409dac11034db1ca358801fdc4 to your computer and use it in GitHub Desktop.
Save xumix/0fad01409dac11034db1ca358801fdc4 to your computer and use it in GitHub Desktop.
Migrate from 301 Url Tracker to Skybrud.Umbraco.Redirects

This is a script that can help you migrate your redirects from 301 Url Tracker to Skybrud.Umbraco.Redirects.

The script is dependent on two pull requests I made (enabling regex'es and forwarding of query strings).

namespace EIIP.Portal.Web.Controllers
{
using System;
using System.Web.Http;
using InfoCaster.Umbraco.UrlTracker.Models;
using Skybrud.Umbraco.Redirects.Models;
using Umbraco.Web.WebApi;
public class MigrateRedirectsController : UmbracoApiController
{
[HttpGet]
public void Migrate(string authKey)
{
if (authKey != "F8AF93CD-1A33-48D9-B443-016984D2761C")
{
return;
}
var urlTrackerEntries = ApplicationContext.DatabaseContext.Database.Fetch<UrlTrackerModel>("SELECT * FROM icUrlTracker");
foreach (var entry in urlTrackerEntries)
{
var row = new RedirectItemRow();
row.Created = entry.Inserted.Ticks;
row.ForwardQueryString = entry.RedirectPassThroughQueryString;
row.IsPermanent = entry.RedirectHttpCode == 301 || entry.RedirectHttpCode == 401;
if (entry.RedirectNodeId != null)
{
var redirectNode = this.Umbraco.TypedContent(entry.RedirectNodeId);
if (redirectNode != null)
{
row.LinkId = redirectNode.Id;
row.LinkName = redirectNode.Name;
row.LinkUrl = redirectNode.Url;
}
else
{
row.LinkId = (int)entry.RedirectNodeId;
row.LinkName = entry.Notes ?? entry.RedirectUrl;
row.LinkUrl = entry.RedirectUrl;
}
}
else
{
row.LinkName = entry.Notes ?? entry.RedirectUrl;
row.LinkUrl = entry.RedirectUrl;
}
var linkmode = entry.RedirectNodeId != null ? RedirectLinkMode.Content : RedirectLinkMode.Url;
row.QueryString = entry.OldUrlQueryString;
row.RootNodeId = Math.Max(0, entry.RedirectRootNodeId);
row.Updated = DateTime.Now.Ticks;
row.IsRegex = !string.IsNullOrEmpty(entry.OldRegex);
row.Url = entry.OldRegex ?? (entry.OldUrl.StartsWith("/") ? string.Empty : "/") + entry.OldUrl;
try
{
RedirectsRepository.Current.AddRedirect(
row.RootNodeId,
row.Url,
new RedirectLinkItem(row.LinkId, row.LinkName, row.LinkUrl, linkmode),
row.IsPermanent,
row.IsRegex,
row.ForwardQueryString);
}
catch (Exception e)
{
// just catch it, in case there a duplicate entries in UrlTracker.
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment