2

sc_itemid Query String – Having a detrimental effect on the SEO

 1 year ago
source link: https://blog.coates.dk/2022/11/11/sc_itemid-query-string-having-a-detrimental-effect-on-the-seo/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
seo.png?w=624

sc_itemid Query String – Having a detrimental effect on the SEO

If the sc_itemid query string has a valid sitecore item ID (and or sitecore path), sitecore ignores the URL path and sets the current item to the specified item.

This is generally used by the sitecore client tools i.e., preview, experience editor, debugging, etc. to specify the current item.

Unfortunately, one customer identified that the use of sc_itemid, was having a detrimental effect on the SEO for their websites, but why was sc_itemid present on their public available websites?

I believed the correct solution was to identify why sc_itemid was present in the URL and correct the issue.

Unfortunately, after identifying several issues relating to editor’s content, legacy system and legacy code, the customer informed me that they could not be resolved/fixed and asked me to find an alternative solution.

Solution

If it is not a sitecore client request and sc_itemid is present make a permanent redirect, following the these rules:

  • If the sc_itemid has a valid item id or path, make a permanent redirect to canonical URL for the item.
  • If the sc_itemid does not have a valid item id or path, strip the sc_itemid query string and make a permanent redirect to that URL.

Step 1 – httpRequestBegin Pipeline Processor

Introduce a pipeline processor to check URL’s and add it to the httpRequestBegin pipeline just before the LayoutResolver processor.

Define which site’s should be ignored, for example the sitecore client typically uses the shell site context.

Define which URL paths should be ignored, for example URL’s starting with /sitecore are usually for the sitecore client, see the config below for more typical examples.

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<httpRequestBegin>
<processor type="Feature.ErrorHandling.Infrastructure.Pipelines.QueryStringPermanentlyRedirectHttpRequestProcessor, Feature.ErrorHandling"
patch:before="processor[@type='Sitecore.Pipelines.HttpRequest.LayoutResolver, Sitecore.Kernel']"
resolve="true">
<IgnoreSites hint="list:AddIgnoreSite">
<!--list of all the sites to ignore-->
<site>shell</site>
<site>admin</site>
<site>login</site>
<site>service</site>
<site>modules_shell</site>
</IgnoreSites>
<IgnorePaths hint="list:AddIgnorePath">
<!--list of all the sites to ignore-->
<site>/sitecore</site>
<site>/api</site>
</IgnorePaths>
</processor>
</httpRequestBegin>
</pipelines>
</sitecore>
</configuration>

Step 2 – Which Requests/URL’s to ignore

The most important part of the any pipeline processors is to ensure that it identifies requests to ignore and exits as soon as possible, as all request go through the httpRequestBegin pipeline.

Therefore the QueryStringPermanentlyRedirectHttpRequestProcessor must check the following and exit if one of the conditions is true.

  • Local path is null
  • Http Context is null
  • The pipeline is aborted
  • The query string sc_itemid is not present
  • The Site context is in the Ignore Sites list (see configuration above)
  • The URL path starts with the path in the ignore paths list (see configuration above)
    • Context.PageMode.IsNormal is not true i.e. It is a sitecore client request – editing, preview, experience, debugging, etc.
  • Is a request for a physical file

Step 3 – Abort the Pipeline & Permanent Redirect

The last step is simple, make the permanent redirect depending on the following logic:

  • If sc_itemid identifies a valid item, get the canonical URL and redirect.
  • If sc_itemid does not identifies a valid item, remove the sc_itemid querystring and redirect to the URL.

Remember to abort the pipeline first before redirecting and catch the ThreadAbortException, see the code below

try
{
args.AbortPipeline();
args.HttpContext.Response.RedirectPermanent(redirectUrl);
}
catch (ThreadAbortException ex)
{
// do nothing, as this is caused by the redirect
}

Hope this helps, Alan


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK