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.
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.
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);
}
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).
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.
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:
If someone tries to visit:
It should redirect to the default culture page instead:
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.
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
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.
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:
Thanks,
Alex
How do I do that to accomplish what I wrote in the thread?
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?:
Besides, I need to substring the two first characters, so I get the true values.
Basically that's what my URLs are.
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.
So I ended up making an awful fix, until I get a better "answer". It goes like this:
So basically, if I try to request:
It will redirect. Example:
(that depends on your locale though).
Not sure how this works when your locale is something else thanen
orda
though.See updated code.
Morten, one more question - how users will know about pages without culture if you will do navigation where all links are with culture prefix?
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).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.
Morten, it shouldn't be DOM problem, I think you got 404 error if URL doesn't exist in Umbraco.
Hi Morten,
Where do we need to add this code? Can you let me know?
Thank you
is working on a reply...