kentico
URL spécifiques à la culture
Recherche…
Configuration des URL spécifiques à la culture
Avoir des URL spécifiques à la culture peut être bénéfique en termes de référencement.
Par exemple, version anglaise de la page suivante:
http://www.mydomain.com/insurance
Cela se traduirait par:
http://www.mydomain.nl/verzekering
Au lieu de:
http://www.mydomain.nl/nl-nl/insurance
Il y a plus d'approches pour y parvenir:
Généralement, vous voulez que les URL soient dérivées des noms des documents. Pour ce faire, assurez-vous de définir l' option Utiliser le chemin d'accès du chemin d'URL sur true. Par défaut, c'est faux.
Si le modèle de création d'URL par défaut ne fonctionne pas pour vous, vous pouvez définir les URL manuellement comme décrit dans la documentation officielle . Cependant, cette option n'est viable que si vous devez ajuster de petites quantités d'URL.
Si vous souhaitez automatiser la création d'URL basées sur un modèle personnalisé, vous pouvez implémenter un module personnalisé .
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();
}
}
- Si vous avez besoin de mettre à jour des pages existantes (par exemple, si vous avez oublié de cocher Utiliser le chemin de nom pour le chemin de l'URL avant de commencer à développer votre projet), vous pouvez utiliser une application console simple pour mettre à jour les 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();
}
}
}