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).
using InfoCaster.Umbraco.UrlTracker.Models;
using Skybrud.Umbraco.Redirects.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using umbraco.BusinessLogic;
using umbraco.DataLayer;
using Umbraco.Web.WebApi;
public class MigrateUrlTrackerToRedirectsController : UmbracoApiController
{
[HttpGet]
public List<RedirectItemRow> Migrate()
{
List<UrlTrackerModel> urlTrackerEntries = new List<UrlTrackerModel>();
List<IParameter> parameters = new List<IParameter>();
string query = "SELECT * FROM icUrlTracker WHERE Is404 = 0"; // remove where clause, if you want to add 404 records too.
using (IRecordsReader reader = Application.SqlHelper.ExecuteReader(query, parameters.ToArray()))
{
while (reader.Read())
{
urlTrackerEntries.Add(new UrlTrackerModel(reader.GetInt("Id"), reader.GetString("OldUrl"), reader.GetString("OldUrlQueryString"), reader.GetString("OldRegex"), reader.GetInt("RedirectRootNodeId"), reader.Get<int?>("RedirectNodeId"), reader.GetString("RedirectUrl"), reader.GetInt("RedirectHttpCode"), reader.GetBoolean("RedirectPassThroughQueryString"), reader.GetBoolean("ForceRedirect"), reader.GetString("Notes"), reader.GetBoolean("Is404"), reader.GetString("Referrer"), reader.GetDateTime("Inserted")));
}
}
urlTrackerEntries = urlTrackerEntries.Where(x => x.RedirectNodeIsPublished).ToList();
//var entries = UrlTrackerRepository.GetUrlTrackerEntries(showAutoEntries: false);
var rows = new List<RedirectItemRow>();
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 = 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;
}
}
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("/") ? "" : "/") + entry.OldUrl;
rows.Add(row);
try
{
RedirectsRepository.Current.AddRedirect(row.RootNodeId, row.Url, new RedirectLinkItem(row.LinkId, row.LinkName, row.LinkUrl, linkmode), row.IsRegex, row.ForwardQueryString);
}
catch(Exception e)
{
// just catch it, in case there a duplicate entries in UrlTracker.
}
}
return rows;
}
}