csharp 从301 Url Tracker迁移到Skybrud.Umbraco.Redirects

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 从301 Url Tracker迁移到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).
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;

	}
}

以上是关于csharp 从301 Url Tracker迁移到Skybrud.Umbraco.Redirects的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Nuxt ServerMiddleware 和 Apollo GraphQL 处理 301 重定向

旧到新的 301 重定向参数以实现更好的 Google 迁移?

迁移后修复404

htaccess 301 与重定向 404

当 url 没有斜杠时,AWS s3 强制 302 重定向 - 需要 301s

301从文件夹重定向到url