Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • sezal 8 posts 89 karma points
    Apr 16, 2021 @ 08:31
    sezal
    0

    UmbracoUrlAlias for Custom url

    Hello, I am using UmbracoUrlAlias for having custom urls and I want the page to be redirected to custom url if UmbracoUrlAlias is set whenever user clicks on any node/link available in web page. User should not see the original url on browser when the umbracoUrlAlias is set in CMS.

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Apr 17, 2021 @ 09:58
    Marc Goodson
    100

    Hi Sezal

    There are a number of ways of handling this scenario...

    The simplest, way is probably to only write out links to content using what you have set for the umbracoUrlAlias

    so if you are building a navigation menu and using @navItem.Url to write out the link, instead have a custom method that checks umbracoUrlAlias of the navItem first, and if it's populated use that instead of the .Url property.

    On the page itself add a Canonical Meta tag with the value of the UmbracoUrlAlias... this means if anyone visits the page on the original default Url, they will still see the page, but there won't be any penalisation in SEO search indexing, as all requests to the Url will be counted underneath the single Canonical Url, eg no need for redirects!

    One consideration you'll have is that umbracoUrlAlias takes a comma delimited list of multiple Urls... so you probably want to split on a , and have the convention that the first UrlAlias in the list is the canonical one!

    If you are determined to have a redirect, the next simplest way is to handle the PublishedRequest Prepared event:

    PublishedRequest.Prepared += PublishedRequest_Prepared;

    https://our.umbraco.com/Documentation/Reference/Routing/Request-Pipeline/inbound-pipeline

    This is fired just before your page is rendered, so you 'could' check the incoming request, and check the content it's matched, and see if it has a populated umbracoUrlAlias page, and instead of allow the page to be rendered, redirect to the umbracoUrlAlias value to start the processing of the request again.

    Thirdly when a request comes into Umbraco, a series of 'IContentFinders' try one after another in a queue to match the Url of the incoming request to a content item in Umbraco.

    https://our.umbraco.com/documentation/reference/routing/request-pipeline/icontentfinder

    If the ContentFinder makes a match, then the rest of the ContentFinders don't get a chance to find a match, and the content item is rendered.

    There IS a ContentFinder called ContentFinderByUrlAlias

    https://github.com/umbraco/Umbraco-CMS/blob/d428a4543f33bb7094cf7db5f6b6fdc2d1de3063/src/Umbraco.Web/Routing/ContentFinderByUrlAlias.cs

    and this executes 'before' ContentFinderByUrl, which matches the original 'default' url for the content item.

    So another option would be to create a custom ContentFinder RedirectOriginalUrlToUrlAliasContentFinder() and copy the ContentFinderByUrl implementation

    https://github.com/umbraco/Umbraco-CMS/blob/d428a4543f33bb7094cf7db5f6b6fdc2d1de3063/src/Umbraco.Web/Routing/ContentFinderByUrl.cs

    but instead of returning the content, you would check for the umbracoUrlAlias value, and put the redirect in there.

    You'd then register your Custom ContentFinder 'before' the ContentFinderByUrl finder in the queue...

    lots of options!, but I usually do the first one!

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft