SEO Friendly URL

Today I would like to discuss regarding SEO Friendly URL issue then will discuss the resolutions:

Every project has to work on SEO (Search Engine Optimization). Particularly I would like to tell you about the URL. As per SEO recommendation, our URL should be in small letters and it should not have space in URLs.

To take care of this problem, we can write Sitecore event handler

protected void HandleItemName(object sender, EventArgs args)
{
try
{
InitializeConfiguration();

Item item = Event.ExtractParameter(args, 0) as Item;

if (PageTemplates.Contains(item.TemplateID.ToString()) &&
item.Database.Name == this.ItemHandlerDatabase &&
item.Paths.Path.StartsWith(this.ItemHandlerParentItemPath) &&
!item.Name.Equals(item.Name.ToLower().Replace(" ", SEOFriendlyCharacter))
)
{

string displalyName = item.Appearance.DisplayName;
if (string.IsNullOrEmpty(displalyName))
{
displalyName = item.Name;
}
using (new SecurityDisabler())
{
item.Editing.BeginEdit();

item.Appearance.DisplayName = displalyName;

item.Name = displalyName.ToLower().Replace(" ", SEOFriendlyCharacter);

item.Editing.EndEdit();
}
}
}
catch (Exception ex)
{
Log.Error("ItemnameHandler -> HandleItemName :", ex, this);
}
}

Create a patch file to configure this handler

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set">
<sitecore>
<events>
<event name="item:added">
<handler type="XXXX.Events.ItemnameHandler, XXXX" method="HandleItemName" />
</event>
<event name="item:renamed">
<handler type="XXXX.Events.ItemnameHandler, XXXX" method="HandleItemName" />
</event>
</events>
<settings>
<setting name="Page_Template_Ids" value="{4EBC52AA-AE5B-4099-96BD-EC631CBDFB93},{00931377-84E1-4061-9A64-AB98EB09B863},{7D6FF33A-C873-4F85-BA14-9DAB6E729BA2}" />
<setting name="PageTemplate_Ids_Delimeter" value="," />
<setting name="ItemHandler_Database" value="master" />
<setting name="SEO_Friendly_Character" value="-" />
<setting name="ItemHandler_ParentItemPath" value="/sitecore/content/Home" />
</settings>
</sitecore>
</configuration>

The above handler would work only if a content author is creating any page. While creating or renaming item/page, it will assign user given name to the item’s display name attribute and after that, it converts the given name into the lower case and replaces the space with the “-” (configured character).

But what if the hundreds of pages have already been created and now have to correct their URL as per the SEO guidelines. The above code will not work in this case.

At this point in time we can have few options:

  1. Change the page name manually. If you have hundreds of page, it is very difficult and time-consuming task. Since you are doing manual, it can be error-prone as well.
  2. Write a Sitecore command and configure it on the control panel. Whenever you will click on the command present on control panel it would run and will correct your URLs. But it will also take some time and have to write C# code on this.
  3. Write a Sitecore Powershell script for this. It’s very quick.

Especially I am very much impressed with Sitecore Powershell script. It saves our live whenever you have to perform the bulk operation on Sitecore items.

Powershell Script for replacing space from the URL and making the URL in small caps:

cd 'master:/content/Home'
Get-ChildItem -Recurse . | ForEach-Object{

if($_.Fields["__Display name"].Value -eq "")
{
$originalName = $_.Name
}
else
{
$originalName = $_.Fields["__Display name"].Value 
}

$newName = $originalName.Replace(" ", "-")
$newName = $newName.ToLower()
Write-Host "Renaming item from " -nonewline;
Write-Host $originalName -f Yellow -nonewline;
Write-Host " to " -nonewline;
Write-Host $newName -f Green;

if($newName -ne "")
{
$_.Editing.BeginEdit()
$_.Name = $newName;
$_.Fields["__Display name"].Value = $originalName;
$_.Editing.EndEdit()
""
}
else
{
Write-Host $orignnalName
}
}

The above code is valid when items under given path have pages only otherwise you can add templateId in the condition with Get-ChildItem.

You can download complete code here.

Thanks !!!!

 

Leave a comment