Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • jakub hromada 8 posts 98 karma points
    Aug 12, 2019 @ 16:38
    jakub hromada
    0

    Redirect Url encoding with trailing slash

    Reaching out to community after many hours of head scratching.

    I have a site that contains a rewrite rule to remove trailing slash from all URLs. This site also has 301 URL Tracker package installed.

    Removal of the trailing slash works OK unless the URL contains both accented character and trailing slash, if it does, the resulting redirect is an incorrect URL.

    Example:

    requested url: http://site.local/old-page/terre-dhermès-edt/

    rewritten url: http://site.local/new-page/terre-dhermès-edt

    Looking at the network tab in chrome I can also see 2 encoded request URLs.

    1st:http://site.local/terre-dherm%C3%A8s-edt/ - 301

    2nd: http://site.local/terre-dherm%C3%83%C2%A8s-edt - 404

    Also, if I remove the rewrite rule from web.config the Url is encoded correctly and the redirect works fine - but obviously trailing slash stays.

    Anyone has experienced anything similar here?

    My trailing slash removal redirect:

    <rule name="RemoveTrailingSlashRule" stopProcessing="true">
        <match url="(.*)/$" />
        <conditions>
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
        <action type="Redirect" url="{R:1}" />
    </rule>
    

    Thanks

    Jakub

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Aug 13, 2019 @ 05:43
    Shaishav Karnani from digitallymedia.com
    1

    Hi Jakub,

    Problem is that you are doing multiple redirects causing the issue as first redirect is encoding and then 2nd one further encode it. You can check it here. http://www.redirect-checker.org/index.php

    I have updated below rule and have added UrlDecode. However, you can try by adding old page to new page rule on top of RemoveTrailingSlashRule or vice versa.

    <rule name="RemoveTrailingSlashRule" stopProcessing="true">
        <match url="(.*)/$" />
        <conditions>
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
        <action type="Redirect" url="{UrlDecode:{R:1}}" />
    </rule>
    

    Let me know how it goes?

  • jakub hromada 8 posts 98 karma points
    Aug 13, 2019 @ 16:10
    jakub hromada
    100

    Thanks for the reply,

    yep you are right, based on the chrome network tab trace, there is definitely double encoding happening.

    Unfortunately, your suggestion didn't help to decode the URL. The final url was still double encoded.

    During investigation, I have tried to replace your suggested UrlDecode with UrlEncode which surprisingly resulted in correctly generated URL with the trailing slash removed... but, all slashes encoded to %2F :/

    Anyway, after another day of puzzling, I have decided to remove the trailing slash removal rewrite rule from web.config and replaced it with a bit of code that hooks into the Umbraco page request to manually remove the trailing slash from URL and all works as expected.

    Thanks for the help.

    Jakub

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Aug 13, 2019 @ 16:43
    Shaishav Karnani from digitallymedia.com
    0

    Hi Jakub,

    Default behaviour of umbraco is adding slash. So, why do you want to remove slash. We have generally used below Canonical code to add slash for all the links in Umbraco to ensure same Canonical for slash and non-slash links.

    String Authority = Request.Url.GetLeftPart(UriPartial.Authority);
    String Canonical = Authority + Model.CurrentPage.Url;
    
    if (!Canonical.EndsWith("/"))
    {
        Canonical = Canonical + "/";
    }
    <link rel="canonical" href="@Canonical" />
    
  • jakub hromada 8 posts 98 karma points
    Aug 14, 2019 @ 10:30
    jakub hromada
    0

    Hi,

    Thanks for the suggestion.

    I have inherited this site and think that the removal of the trailing slash was just a requirement.

    Jakub

Please Sign in or register to post replies

Write your reply to:

Draft