Copied to clipboard

Flag this post as spam?

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


  • kalpesh boghara 4 posts 74 karma points
    Feb 03, 2023 @ 08:44
    kalpesh boghara
    0

    Visitor Redirection based on country

    Hello All,

    we need to redirect page base on visitor country.

    I have one link and if visitor click that link and if visitor from India then redirect to : /en_in/product1/product2/prodcut3/Product

    and if visitor from USA then redirect to : /en_us/product1/product2/prodcut3/Product

    And another points is like both page data/contain will be not same.

    So please help me how we can archive this type of custom URL structure into Umbraco 11

    Thanks!

  • Roy Berris 89 posts 576 karma points c-trib
    Feb 03, 2023 @ 09:05
    Roy Berris
    0

    Hi,

    Not sure on how you would do the first part. Knowing where the user is from. But I can give you a direction on how to achieve the URL structure.

    You'll need to write custom URL providers:

    https://docs.umbraco.com/umbraco-cms/reference/routing/request-pipeline/outbound-pipeline#custom-url-provider

    And content finders:

    https://docs.umbraco.com/umbraco-cms/reference/routing/request-pipeline/icontentfinder

    Probably best to start at the root of this documentation section: https://docs.umbraco.com/umbraco-cms/reference/routing/request-pipeline

    Good luck!

  • Julien Kulker 75 posts 427 karma points c-trib
    Feb 03, 2023 @ 15:13
    Julien Kulker
    0

    Hi there,

    So i did something similar for a customer but this was related to a component on the page.

    First we need to have offcourse the ip of the visitor.

    var clientIpAddress = this.HttpContextAccessor?.HttpContext?.Connection?.RemoteIpAddress;
    

    Then lets create a RenderController for the DocumentType "Product"

    public class ProductController : RenderController
    {
        public override IActionResult Index()
        {
            var clientIpAddress = this.HttpContextAccessor?.HttpContext?.Connection?.RemoteIpAddress;
            var ipInfoUrl = string.Format("http://ipinfo.io/{0}", clientIpAddress);
            var request = await httpClient.GetAsync(new Uri(ipInfoUrl ));
            var result = await request.Content.ReadAsStringAsync();
            if (result is not null)
            {
                 var ipInfo = JsonConvert.DeserializeObject<dynamic>(result);
                 var countryCode = ipInfo?.Country;
    
                 // So now we have the country code so now you can build logic to render right product
                 if(countryCode == "IN") {
                          // find your indian product
                 }
             }
            // return a 'model' to the selected template/view for this page.
            return CurrentTemplate(CurrentPage);
        }
    }
    

    So when a visitor visit a page with DocumentType "Product" Then the custom ProductController will hit.

    With a simple call to IP info we find out where the visitor is from.

    I hope this will help you forward

Please Sign in or register to post replies

Write your reply to:

Draft