Zoeken…


Cultuurspecifieke URL's configureren

Het hebben van cultuurspecifieke URL's kan voordelig zijn in termen van SEO.

Bijv. Engelse versie van de volgende pagina:

http://www.mydomain.com/insurance

Zou vertalen in:

http://www.mydomain.nl/verzekering

In plaats van:

http://www.mydomain.nl/nl-nl/insurance

Er zijn meer benaderingen om dit te bereiken:

  1. Meestal wilt u dat de URL's worden afgeleid van de namen van documenten. Zorg er daarom voor dat u Naampad voor URL-pad gebruiken instelt op true. Standaard is het vals. Gebruik naampad voor URL-pad

  2. Als het standaardpatroon voor het maken van URL's niet voor u werkt, kunt u de URL's handmatig instellen zoals beschreven in de officiële documentatie . Deze optie is echter alleen bruikbaar als u kleine hoeveelheden URL's moet aanpassen.

  3. Als u het maken van URL's op basis van een aangepast patroon wilt automatiseren, kunt u een aangepaste module implementeren.

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();
    }
}
  1. Als u bestaande pagina's moet bijwerken (bijv. Als u bent vergeten het selectievakje Naampad gebruiken voor URL-pad voordat u begon met het ontwikkelen van uw project), kunt u een eenvoudige consoletoepassing gebruiken die de URL's voor u bijwerkt:
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
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow