London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Do a 301 from simple address to standard url?

Vote:
0

Hi,

Is it possible to make a 301 redirect to a pages full url when someone is making a request to a simple address?

Thanks!

#117931
Feb 24, 2015 15:42
Vote:
1

Hi,

Sure you can. Put the following code in your template's base class and call the method from page load event (or similar):

private void RedirectSimpleAddress()
{
    if (PageEditing.PageIsInEditMode)
    {
        return;
    }

    string simpleAddress = this.CurrentPage.ExternalURL;

    if (!string.IsNullOrWhiteSpace(simpleAddress))
    {
        var requestedPath = this.Request.RawUrl;

        if (!simpleAddress.StartsWith("/"))
        {
            simpleAddress = string.Concat("/", simpleAddress);
        }

        simpleAddress = VirtualPathUtility.RemoveTrailingSlash(simpleAddress);
        requestedPath = VirtualPathUtility.RemoveTrailingSlash(requestedPath);

        if (requestedPath.Equals(simpleAddress, StringComparison.OrdinalIgnoreCase))
        {
            this.Response.RedirectPermanent(this.CurrentPage.ExternalUrl(true));
        }
    }
}

ExternalUrl(true) is an extension method to retrieve the url of the page, which I didn't include in this code example. This is untested code, but something similar should work.

#117940
Feb 24, 2015 18:58
Vote:
0

Thanks Johan! That pointed me in the right direction. I'm using MVC so I put a similar method in a IResultFilter's OnResultExecuting method and it works perfect! :-)

#118448
Mar 06, 2015 11:20
Vote:
0

Hi Daniel,

can you please post the sample code how you did this.. a client wants this also.. 

thanks in advance

#133262
Aug 31, 2015 15:19
Vote:
1
public class PageContextActionFilter : IResultFilter
    {
        private readonly PageViewContextFactory _contextFactory;
        public PageContextActionFilter(PageViewContextFactory contextFactory)
        {
            _contextFactory = contextFactory;
        }

        public void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var viewModel = filterContext.Controller.ViewData.Model;

            var model = viewModel as IPageViewModel<BlankPageData>;
            if (model != null)
            {
                var currentContentLink = filterContext.RequestContext.GetContentLink();

                model.CurrentPage.PageRouteValues = filterContext.RequestContext.GetPageRoute(currentContentLink);

                RedirectIfCurrentUrlIsSimpleAddress(filterContext.RequestContext, model.CurrentPage);

                var layoutModel = model.Layout ?? _contextFactory.CreateLayoutModel(currentContentLink, filterContext.RequestContext, model.CurrentPage);

                var layoutController = filterContext.Controller as IModifyLayout;
                if (layoutController != null)
                {
                    layoutController.ModifyLayout(layoutModel);
                }

                model.Layout = layoutModel;

                if (model.Section == null)
                {
                    model.Section = _contextFactory.GetSection(currentContentLink);
                }
            }
        }

        private void RedirectIfCurrentUrlIsSimpleAddress(RequestContext requestContext, BlankPageData currentPage)
        {
            if (PageEditing.PageIsInEditMode)
                return;

            string simpleAddress = currentPage.ExternalURL;

            if (!string.IsNullOrWhiteSpace(simpleAddress))
            {
                var requestedPath = requestContext.HttpContext.Request.RawUrl;

                if (!simpleAddress.StartsWith("/"))
                {
                    simpleAddress = string.Concat("/", simpleAddress);
                }

                if (!simpleAddress.EndsWith("/"))
                {
                    simpleAddress = string.Format("{0}/", simpleAddress);
                }

                if (string.IsNullOrEmpty(requestedPath))
                    return;

                if (requestedPath.Equals(simpleAddress, StringComparison.OrdinalIgnoreCase))
                {
                    // Should redirect
                    var fullUrl = currentPage.PageLink.GetUrl();

                    if (!string.IsNullOrEmpty(fullUrl))
                    {
                        if (fullUrl == simpleAddress)
                            return;

                        requestContext.HttpContext.Response.RedirectPermanent(fullUrl);
                    }

                }
            }
        }

        public void OnResultExecuted(ResultExecutedContext filterContext)
        {
        }
    }

Then you'll need to register it in an IInitalizableModule,

[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class FilterConfig : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            GlobalFilters.Filters.Add(ServiceLocator.Current.GetInstance<PageContextActionFilter>());
        }

        public void Uninitialize(InitializationEngine context)
        {
        }

        public void Preload(string[] parameters)
        {
        }
    }


Some of the above code is specific for my solution but I'll hope it will get you in the right direction. Let me know if you have any questions.

#133263
Edited, Aug 31, 2015 15:36
Vote:
0

Thanks man...

Because i didnt use the PageContextActionFilter from the Alloy demo and i dont use the ModifyLayout stuff, i added this to my PageControllerBase<T>  OnActionExecuting method and this works perfectly..

#133264
Aug 31, 2015 16:34
Vote:
0

I'm glad it helped you! :-)

#133265
Aug 31, 2015 17:04
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.