Copied to clipboard

Flag this post as spam?

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


  • Paul 184 posts 646 karma points
    Nov 13, 2015 @ 14:25
    Paul
    0

    Canonical URL

    Hi all,

    Just a quick note to mention that the code on this page didn't work for me in Umbraco 7.3.1 : https://our.umbraco.org/wiki/reference/code-snippets/razor-snippets/canonicalurl/

    Here's some revised code that does work in Umbraco 7.3.1 though

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
     @{var canonicalUrl= String.Empty;}
     @if(umbraco.library.RequestServerVariables ("HTTP_HOST").ToLower().StartsWith("www")) {
     canonicalUrl = string.Concat("http://",  umbraco.library.RequestServerVariables("HTTP_HOST"), Model.Content.Url);
     } else {
     canonicalUrl = string.Concat("http://www.",  umbraco.library.RequestServerVariables("HTTP_HOST"), Model.Content.Url);
     }
     "@canonicalUrl"
    

    Hopefully that will help anyone who was looking how to do this as I was!

  • Per Olsson 47 posts 307 karma points
    Nov 13, 2015 @ 14:43
    Per Olsson
    1

    I would actually suggest adding a 301 Redirect instead in Umbraco's UrlRewriting.config.

    <add name="301Redirect_For_WWW_Domain"
         redirectMode="Permanent"
         redirect="Domain"
         ignoreCase="true" 
         rewriteUrlParameter="IncludeQueryStringForRewrite"
         virtualUrl="http://example.com/(.*)"
         destinationUrl="http://www.example.com/$1" />
    
  • Paul 184 posts 646 karma points
    Nov 16, 2015 @ 15:54
    Paul
    0

    How does adding a 301 redirect provide a canonical link variable ?

    Unless I'm missing something I can't see how what you posted relates.

  • Per Olsson 47 posts 307 karma points
    Nov 18, 2015 @ 07:47
    Per Olsson
    100

    Hi Paul,

    The script you posted only makes sure the url always uses "www". A canonical url is dependent on the client and that it understands the canonical tag. A 301 will forcefully redirect any request from non-www to www at the server level (IIS).

    Googles recomendation:

    While the canonical link element has its benefits, Matt Cutts, the head of Google's webspam team, has claimed that the search engine prefers the use of 301 redirects. Cutts claims the preference for redirects is because Google's spiders can choose to ignore a canonical link element if they feel it is more beneficial to do so.[8]

    https://en.wikipedia.org/wiki/Canonicallinkelement (for some reason the forum breaks the link, just check the wikipedia article for "Canonical link element")

  • Paul 184 posts 646 karma points
    Nov 20, 2015 @ 13:44
    Paul
    0

    Thanks Per, I wasn't aware you could do it like that (using 301 redirects).

    Many thanks :-)

Please Sign in or register to post replies

Write your reply to:

Draft