kentico
문화권 별 URL
수색…
문화권 관련 URL 구성
문화권 관련 URL을 보유하면 검색 엔진 최적화 측면에서 도움이됩니다.
다음 페이지의 영어 버전 :
http://www.mydomain.com/insurance
번역 대상 :
http://www.mydomain.nl/verzekering
대신에:
http://www.mydomain.nl/nl-nl/insurance
이를 달성하기위한 더 많은 접근 방법이 있습니다.
일반적으로 URL을 문서의 이름에서 파생시킬 수 있습니다. 이렇게하려면 URL 경로 에 이름 경로 를 true 로 설정 하십시오 . 기본적으로 false입니다.
기본 URL 작성 패턴이 작동하지 않으면 공식 문서에 설명 된대로 수동으로 URL을 설정할 수 있습니다. 그러나이 옵션은 소량의 URL을 조정해야하는 경우에만 사용할 수 있습니다.
사용자 정의 패턴을 기반으로 URL 작성을 자동화하려는 경우 사용자 정의 모듈을 구현할 수 있습니다.
using System;
using System.Text;
using CMS;
using CMS.DataEngine;
using CMS.DocumentEngine;
using CMS.Helpers;
[assembly: RegisterModule(typeof(CultureSpecificUrlsModule))]
public class CultureSpecificUrlsModule : Module
{
public CultureSpecificUrlsModule() : base("CultureSpecificUrlsModule")
{
}
protected override void OnInit()
{
base.OnInit();
/***
* Before the node gets saved, we'll update it's DocumentUrlPath.
* The system will ensure it'll be saved in a valid URL format.
*/
DocumentEvents.Update.Before += Update_Before;
}
private void Update_Before(object sender, DocumentEventArgs e)
{
/***
* Here you can apply conditions before you actually update the DocumentUrlPath.
* E.g. you can check for the document's culture.
*/
UpdateUrlPath(e.Node);
}
public static void UpdateUrlPath(TreeNode node)
{
/***
* You can set the DocumentUrlPath to whatever you want.
* In this example we're using a method extracted from CMS.DocumentEngine.TreePathUtils.
* The same method is used to generate a URL for the default culture.
*/
node.DocumentUrlPath = GetUrlPathFromNamePathInternal(node.DocumentNamePath);
}
internal static string GetUrlPathFromNamePathInternal(string namePath, int level = -1)
{
// Check valid path
if (String.IsNullOrEmpty(namePath) || (namePath == "/"))
{
return null;
}
// For top level the path is always /
if (level == 0)
{
return "/";
}
// Ensure maximal level if not set
if (level < 0)
{
level = Int32.MaxValue;
}
// Get the path parts
string[] pathParts = namePath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
int currentLevel = 1;
var path = new StringBuilder();
foreach (string part in pathParts)
{
string shortPart = part;
// Shorten the part to the allowed maximum
if (shortPart.Length > TreePathUtils.MaxAliasLength)
{
shortPart = shortPart.Substring(0, TreePathUtils.MaxAliasLength);
}
path.Append("/", shortPart);
if (++currentLevel > level)
{
break;
}
}
return path.ToString();
}
}
- 기존 페이지를 업데이트해야하는 경우 (예 : 프로젝트 개발을 시작하기 전에 URL 경로에 이름 경로 사용 확인을 잊어 버린 경우) URL을 업데이트하는 간단한 콘솔 응용 프로그램을 사용할 수 있습니다.
using System;
using CMS.DataEngine;
using CMS.DocumentEngine;
namespace CultureUrlsUtil
{
class Program
{
static void Main(string[] args)
{
CMSApplication.Init();
/*** Here you can narrow down the scope of documents that should be updated using DocumentQuery ***/
var pages = DocumentHelper.GetDocuments().Culture("es-es");
foreach (var page in pages)
{
/*** Here we are calling code from the example above. ***/
CultureSpecificUrlsModule.UpdateUrlPath(page);
page.Update();
}
Console.Write("URLs created!");
Console.ReadLine();
}
}
}
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow