Copied to clipboard

Flag this post as spam?

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


  • Morten 105 posts 345 karma points
    Apr 25, 2017 @ 07:55
    Morten
    0

    Force culture/multilang page when trying to visit non-culture page

    Is it possible to force a redirect to the default multi language page for all pages, if someone tries to visit the page without the multi language part in it?

    Here's an example:

    website.com/en/news
    website.com/da/news
    

    If someone tries to visit:

    website.com/news
    

    It should redirect to the default culture page instead:

    website.com/en/news
    

    I'm guessing something like this should be rather easy (as I assume it's something most users want), but I can't figure out how.

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Apr 25, 2017 @ 11:25
    Alex Skrypnyk
    0

    Hi Morten

    Did you try to use Rewrite rules?

    Look please how to do it: https://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

    You can add Regex rules for your rewrites.

    Thanks,

    Alex

  • Morten 105 posts 345 karma points
    Apr 25, 2017 @ 11:29
    Morten
    0

    Hi Alex

    Yes, I have tried to use rewrite rules to do this, but I'm not quite sure how I'm going to accomplish redirecting ALL pages that are NOT in a culture. I can't think how that regex is going to look or how I'm supposed to make it dynamically redirect.

    For instance: If I want to make it redirect to the user's locale (if you're from Denmark it should redirect to /da/ for example), I would have to do that through JavaScript... Right? I would prefer that over the default culture/language.

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Apr 25, 2017 @ 11:32
    Alex Skrypnyk
    0

    Morten, javascript is not a good way to do it definitely.

    Try to do it with C#

    Check current culture and then redirect to needed URL:

    CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentUICulture;
    

    Thanks,

    Alex

  • Morten 105 posts 345 karma points
    Apr 25, 2017 @ 11:47
    Morten
    1

    How do I do that to accomplish what I wrote in the thread?

    System.Threading.Thread.CurrentThread.CurrentUICulture
    

    Wouldn't that return the culture for server Umbraco is installed on? To get the user's current selected culture, I would do this, right?:

    @using System.Globalization
    CultureInfo.CurrentCulture.ToString()
    

    Besides, I need to substring the two first characters, so I get the true values.

    da-DK => da
    en-US => en
    

    Basically that's what my URLs are.

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Apr 25, 2017 @ 12:48
    Alex Skrypnyk
    0

    Morten, I would like to have some like checking in C# in pre-request event, is CurrentCulture set?

    If no - define what culture we need for redirect and redirect.

  • Morten 105 posts 345 karma points
    Apr 25, 2017 @ 12:49
    Morten
    100

    So I ended up making an awful fix, until I get a better "answer". It goes like this:

    var culture = HttpContext.Current.Request.Url.AbsolutePath.Split('/')[1];
    if (culture != "en" && culture != "da")
    {
        var shortCulture = "en";
    
        var languages = HttpContext.Current.Request.UserLanguages;
    
        //Just making sure
        if (languages != null && languages.Length > 0)
        {
            shortCulture = languages[0].Trim().Substring(0, 2);
    
            //If the culture is something like fr (French), we default back to en
            if (shortCulture != "en" && shortCulture != "da")
            {
                shortCulture = "en";
            }
        }
        Uri uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri);
        var url = uri.Scheme + Uri.SchemeDelimiter + uri.Host;
        url += "/" + shortCulture + HttpContext.Current.Request.Url.AbsolutePath;
        Response.RedirectPermanent(url);
    }
    

    So basically, if I try to request:

    https://website.com/news
    http://website.com/news
    website.com/news
    http://website.com/news/some-news-article/
    etc. etc.
    

    It will redirect. Example:

    https://website.com/news/
    |
    V
    https://website.com/en/news/
    

    (that depends on your locale though).

    Not sure how this works when your locale is something else than en or da though.

    See updated code.

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Apr 25, 2017 @ 12:57
    Alex Skrypnyk
    0

    Morten, one more question - how users will know about pages without culture if you will do navigation where all links are with culture prefix?

  • Morten 105 posts 345 karma points
    Apr 25, 2017 @ 12:59
    Morten
    0

    There are no pages without culture on my site. The only way people are going to access these sites, is if they remove the /en/ or /da/ from the URL (or if Google somehow manages to ignore my SEO).

  • Morten 105 posts 345 karma points
    Apr 25, 2017 @ 13:06
    Morten
    0

    Hmm, just realized this only works if the DOM (or page I guess) actually loads, meaning my example actually does not work (with/without the culture in the URL). However, since it's actually a 404 error, I guess that's perfectly fine. If the URL you enter is invalid, you're on your own haha.

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Apr 25, 2017 @ 13:42
    Alex Skrypnyk
    0

    Morten, it shouldn't be DOM problem, I think you got 404 error if URL doesn't exist in Umbraco.

  • Raj 24 posts 94 karma points
    Feb 01, 2023 @ 17:38
    Raj
    0

    Hi Morten,

    Where do we need to add this code? Can you let me know?

    Thank you

Please Sign in or register to post replies

Write your reply to:

Draft