现在的位置: 首页 > 综合 > 正文

sitecore的link manager(多语言站点注目)

2012年12月05日 ⁄ 综合 ⁄ 共 2491字 ⁄ 字号 评论关闭

在上一篇随笔中记录了“多语言”的图片机制,那样的好处显而易见,但实际上却引入了另一个问题。

 

在讨论该问题之前,先要做一点说明。Sitecore在默认设置下,当前浏览对话的语言偏好是保存在cookie里的。

换句话说,有可能我们浏览的同一个页面的不同的语言版本,在浏览器中的URL是一模一样的。 

 

现在我们来看问题: 

 

假如我们现在有一个产品在如下的URL:

www.somesite.com/product1.aspx

 

在sitecore的preview中它很正常,但当我们在preview里把语言版本切换成中文时会发现,所有的文字内容都正确的切换了版本,但图片依旧是英文版。而按一下F5刷新以后,图片才会变成正确的中文版。

 

很明显,图片被浏览器缓存在本地了。因为URL没有变,浏览器认为我们在访问同一个页面,自然的就将已经存在于本地缓存的图片直接显示,而没有再从服务器上取新的版本。除非我们人为的刷新页面。

 

有很多办法可以解决这个问题,其中一个比较好的办法我认为是将语言版本反应在URL中,实际上这也是SEO Friendly的做法。如:

同样对于Product1,英文版的URL应该是:

www.somesite.com/en/product1.aspx

中文版的则是:

www.somesite.com/zh-cn/product1.aspx

 

这样一来我们解决了上述图片被缓存的问题,二来我们也顺手做了一个重要的SEO优化。

 

那么怎么达成这个URL重写?实际上sitecore有很方便的方法可以直接搞定。

 

打开sitecore网站根目录的Web.config文件,找到 /configuration/sitecore/linkManager/providers节点,默认应该有如下配置:

        <clear/>
        <add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="true" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="asNeeded" languageLocation="filePath" shortenUrls="true" useDisplayName="false"/>

在这里我们看到

 languageEmbedding 默认是 "asNeeded",将其改成"always",并确保

languageLocation="filePath" ,则sitecore生成的所有url都将如上述例子一般,在域名后直接插入语言代码了。

 

 

附sitecore文档中对该配置的详细解释:

 

 Dynamic Link Configuration

You can control friendly URLs by setting the following attributes of the

/configuration/sitecore/providers/add element in web.config with name sitecore.

type: You can override the link provider by setting the type attribute to the appropriate .NET

class signature.

 

addAspxExtension: Whether to include the .aspx extension in URLs (true or false). If you set addAspxExtension to false, you muse configure IIS to process all requests with ASP.NET as described in the section IIS and ASP.NET URLs.

alwaysIncludeServerUrl: Whether to include the HTTP protocol and domain (http://localhost) in friendly URLs (true or false).

encodeNames: Whether to encode names in paths according to the /configuration/sitecore/encodeNameReplacements/replace elements in web.config (true or false).

languageEmbedding: Whether to include the language in the URL (always, never, or asNeeded). When languageEmbedding is asNeeded, Sitecore includes the language in the URL if it cannot determine the context site from the incoming HTTP request, if that HTTP request does not include a cookie that specifies a language, or if the language of the linked item differs from the context language.

languageLocation: Whether to specify language as the first step in the URL path or using the sc_lang URL query string parameter (filePath or queryString).

useDisplayName: Whether to use item display names or item names when constructing URLs (true or false). If the useDisplayName attribute is true, different languages can have different URLs for the same content item.

【上篇】
【下篇】

抱歉!评论已关闭.