Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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.
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.
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.
Try to do it with C#
Check current culture and then redirect to needed URL:
CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentUICulture;
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.
da-DK => da
en-US => en
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:
var culture = HttpContext.Current.Request.Url.AbsolutePath.Split('/');
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.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;
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 than en or da 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.
Where do we need to add this code? Can you let me know?
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted