Vulnerability in EPiServer.Forms

Try our conversational search powered by Generative AI!

Enforce canonical URLs

Vote:
 

Hi,

For a multilingual and multidomain EPiServer 7.5 CMS website (version 7.19.2.0), I have an issue with resolving canonical URLs for content pages.

I would like to enforce that visitors to be redirected to the canonical URL of an EPiServer page.

To accomplice this I have an IInitializableHttpModule which is responsible for resolving the URL and redirecting the browser.

However, visitors do not always get redirected to the right URL.

Our setup:

[host].com/[url]/?[query] should HTTP 301 redirect to [host].com/en/[url]/?[query]
[host].be/[url]/?[query] should HTTP 301 redirect to [host].com/be/[url]/?[query]
[host].nl/[url]/?[query] should HTTP 301 redirect to [host].com/nl/[url]/?[query]

The issue:

In some cases when I request http://[host].nl/ I get redirected to http://[host].com/en/ instead of http://[host].com/nl/.

This seems to be caused by Global.UrlRewriteProvider.ConvertToExternal setting the language to English as instead of Dutch.

[InitializableModule]
    [ModuleDependency(typeof(InitializationModule))]
    public class RequestHandlerInitialization : IInitializableHttpModule
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(RequestHandlerInitialization));

        public void Initialize(InitializationEngine context)
        {
        }

        public void InitializeHttpEvents(HttpApplication application)
        {
            application.BeginRequest += BeginRequest;
        }

        public void Uninitialize(InitializationEngine context)
        {
        }

        public void Preload(string[] parameters)
        {
        }

        private bool ShouldRedirect(string correctUrl, string urlWithoutQuery)
        {
            return correctUrl != urlWithoutQuery;
        }

        private string GetCorrectUrl(UrlBuilder url)
        {
            return url.Uri.IsAbsoluteUri ? url.Uri.AbsoluteUri : url.Uri.OriginalString;
        }

        private void BeginRequest(object sender, EventArgs e)
        {
            try
            {
                HttpContext context = HttpContext.Current;

                string browserUrl = context.Request.Url.ToString();
                UrlBuilder url = new UrlBuilder(context.Request.Url);

                object contentReference;
                Global.UrlRewriteProvider.ConvertToInternal(url, out contentReference);

                // Only redirect for ContentReferences
                if (contentReference is ContentReference)
                {
                    Global.UrlRewriteProvider.ConvertToExternal(url, null, System.Text.Encoding.UTF8);
                    var correctUrl = GetCorrectUrl(url);

                    if (ShouldRedirect(correctUrl, browserUrl))
                    {
                        context.Response.RedirectPermanent(correctUrl, true);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Could not rewrite or check URL", ex);
            }
        }
    }



Does anyone have any clues on what can be done to fix this?

#121967
May 22, 2015 10:51
Vote:
 

There is better support for canonical url handling in version 8.* as described here http://thisisnothing.nystrom.co.nz/2015/02/12/improved-support-for-canonical-urls/

#121970
May 22, 2015 13:40
Vote:
 

Hi Johan,

The article you mentioned is about the canoncal url meta tag instead of browser redirection.

In the meantime we have taken a different approach with the URL Rewrite module for IIS. The module also works in Azure Websites. This way we don't need the code solution and works fine.

To configure the module, we have included this in the web.config:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Default" patternSyntax="Wildcard" stopProcessing="true">
        <match url="*" />
        <action type="Redirect" url="http://www.host.com/{R:0}" />
        <conditions>
          <add input="{HTTP_HOST}" pattern="www.host.nl" />
        </conditions>
      </rule>
    </rules>
  </rewrite>
</system.webServer>

During debugging we also found out that Global.UrlRewriteProvider is not very stable regarding determining the language based on url. The FriendlyUrlRewriteProvider does a better job at this.

The issue becomes visible when calling ConvertToInternal multiple times. As a result, the epslanguage parameter does not always have a value.

I hope this helps others having the same issues.

#122273
May 29, 2015 9:28
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.