Epicweb-Optimizely / Epicweb.Optimizely.RedirectManager

This .net 6 library contains a RedirectManager and admin user interface integration in an Optimizely CMS 12 and commerce 14 project. Tested with Alloy.
Apache License 2.0
0 stars 2 forks source link

Epicweb.Optimizely.RedirectManager

This .net 6 library contains a RedirectManager and admin user interface integration in an Optimizely CMS 12 and commerce 14 project. Tested with Alloy.

Platform Platform Twitter Follow

An Optimizely addon that helps with managements of redirects. Simple but yet so effective. It is based out of https://github.com/huilaaja/RedirectManager

This is the .net 6 version of : https://github.com/huilaaja/RedirectManager <-- use this for CMS 11

Preview:

alt text

Features

Preview:

alt text

alt text

alt text

Installation and configuration

Available on nuget.optimizely.com https://nuget.optimizely.com/package/?id=Epicweb.Optimizely.RedirectManager

How to get started?

Start by installing NuGet package:

Install-Package Epicweb.Optimizely.RedirectManager

Add to startup.cs

services.AddRedirectManager(
    addQuickNavigator: true, 
    enableChangeEvent: true);

also

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    //remember if you use env.IsDevelopment() do activate error pages in dev env too
    //do NOT use => app.UseStatusCodePagesWithRedirects("/Error/{0}");//not redirects
    app.UseStatusCodePagesWithReExecute("/Error/{0}");
    app.UseExceptionHandler("/Error/500");
}

Run your application

First time, you will be prompted to create the redirect table "SEO_redirect"

alt text

Upgraded from .netFramework 4?

That should not be a problem. If you used the Solita solution, change the name of the table "SOLITA_Redirect" to "SEO_Redirect", should be the same schema if you run on latest solution, make sure you have run this V2 upgrade before, (or added the host column manually) [https://github.com/huilaaja/RedirectManager/blob/c7ec6ea4b12aa36b53b27fa89bb373286fe0d53d/WebProject/Redirects/RedirectService.cs#L282]

Schema should look like this:

image

Add code to 404 handler

add this code into your error/404 custom page controller

            #region RedirectManager
            if (statusCode == 404)
            {
                string originalRelativePath = HttpContext.Request.GetRawUrl();//get current url
                string redirectTo = _redirectService.GetPrimaryRedirectUrlOrDefault(SiteDefinition.Current.Name, originalRelativePath);//check if redirect rule exists
                if (redirectTo != null)
                {
                    Response.Redirect(redirectTo, true);
                }
            } 
            #endregion

Complete example of ErrorController

https://github.com/Epicweb-Optimizely/Epicweb.Optimizely.RedirectManager/tree/main/Alloy/Features/Error

using Epicweb.Optimizely.RedirectManager;
using EPiServer.Web;
using Microsoft.AspNetCore.Mvc;

namespace Epicweb.Optimizely.Blog.Features.Error
{
    public class ErrorController : Controller
    {
        private readonly IContentRepository _contentRepository;
        private readonly RedirectService _redirectService;

        public ErrorController(IContentRepository contentRepository, RedirectService redirectService)
        {
            _contentRepository = contentRepository;
            _redirectService = redirectService;
        }
        [Route("/Error/{statusCode}")]
        public IActionResult HttpStatusCodeHandler(int statusCode)
        {
            ViewBag.Code = statusCode;
            //this is specific redirectManager
            #region RedirectManager
            if (statusCode == 404)
            {
                string originalRelativePath = HttpContext.Request.GetRawUrl();//get current url
                string redirectTo = _redirectService.GetPrimaryRedirectUrlOrDefault(SiteDefinition.Current.Name, originalRelativePath);//check if redirect rule exists
                if (redirectTo != null)
                {
                    Response.Redirect(redirectTo, true);
                }
            } 
            #endregion
            return View("~/Features/Error/Error.cshtml");
        }
    }
}

Roles and restrictions

Users with role WebAdmins and RedirectManagers will automatically see the menu in Optimizely CMS

Sandbox alloy app

Get this solution runing

  1. Clone it

  2. Unpack the /alloy/app_data/blobs-and-database.zip

  3. dotnet run

  4. log in to CMS with "admin" and "Test1234!"

Package maintainer

https://github.com/lucgosso