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);
}
基本上,如果我尝试请求:
https://website.com/news
http://website.com/news
website.com/news
http://website.com/news/some-news-article/
etc. etc.
它会重定向 . 例:
https://website.com/news/
|
V
https://website.com/en/news/
2 回答
所以我最终做了一个糟糕的修复,直到我得到一个更好的“答案” . 它是这样的:
基本上,如果我尝试请求:
它会重定向 . 例:
(这取决于你的语言环境) .
可怕,可怕,可怕 . 我很惭愧 .
好的,所以在Umbraco 7中,您可以使用ContentFinder执行此操作 . 这是作为内容管道的一部分运行的东西,它将尝试找到Umbraco自己无法找到的内容 .
内容查找器有两个部分,实际的ContentFinder本身,然后你必须在Umbraco的应用程序启动事件中连接它 .
我实际上做了类似于你正在尝试的东西,但反之(我的所有内容都在/ content,但我使用/ en-en / content等虚拟URL,并在内容查找器中映射所有内容) .
有关ContentFinders的信息,请查看文档:https://our.umbraco.org/documentation/reference/routing/request-pipeline/icontentfinder
在这里's a gist I found that does pretty much what you'之后:https://gist.github.com/alindgren/4f8e47d9b2d769137be3
ContentFinder的优势在于它只会针对未找到的内容运行,而不是像现在的解决方案那样在每个页面加载上运行,从而提高效率!
希望有所帮助!
:)