Copied to clipboard

Flag this post as spam?

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


  • bayshield 50 posts 65 karma points
    Mar 23, 2009 @ 08:06
    bayshield
    0

    How to get current URL for Canonical Tags

    Hi,
    I am migrating my site from asp.net to umbraco and have now setup my pages to work without the .aspx extension.

    Because the pages can still be shown if you add the .aspx extension, I would like to add a canonical tag which basically tells google that the two versions of the page are the same (thereby avoiding duplicate content issues.

    The format of the tag needs to be

    This means that if google was to find the .aspx version of the page it would index it as per the details in the canonical tag.


    I would like to put this in my Master template so my question is what variable can I use to get the href portion of the tag above?


    Thanks,
    Aaron

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Mar 23, 2009 @ 11:42
    Dan Diplo
    0

    In ASP .NET / C# you can use the Request.Url.AbsoluteUri page property to get the full URL of the current page. There are also many other properties of Request.Url for getting host names etc.

    See http://msdn.microsoft.com/en-us/library/system.web.httprequest.url.aspx

    Also see http://www.mikesdotnetting.com/Article.aspx?ArticleID=29 for other methods.

  • bayshield 50 posts 65 karma points
    Mar 23, 2009 @ 17:41
    bayshield
    0

    Thanks for the quick reply Diplo.

    I know about the Request.URL method but my question would be how can I access the value of that in my template?

    Cheers
    Aaron

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Mar 23, 2009 @ 18:43
    Dirk De Grave
    0

    Hi Aaron,

    Add some inline c# code using <% and %> tags as you would in any asp.net application.

    [code]<%
    string url = Request.Url;
    Response.Write(url);
    %>
    [/code]

    Regards,
    /Dirk

  • bayshield 50 posts 65 karma points
    Mar 23, 2009 @ 21:17
    bayshield
    0

    Thanks for all the responses.

    The inline code did write the values out, however the markup was going before the opening tag and I couldn't figure out how to stop it.

    In the end I created a user control with a literal control and the following code:
    [code]
    protected void Page_Load(object sender, EventArgs e)
    {
    string url = Request.Url.ToString();
    if (url.ToLower().EndsWith(".aspx"))
    {
    url = url.Substring(0, url.Length - 5);
    }
    litCanonical.Text ="";
    }
    [/code]

    This simply strips off the .aspx portion of my URL's so I won't run the risk of duplicate content in Google. I will probably extend it in the near future to allow me to override the default value with a value from my template.


Please Sign in or register to post replies

Write your reply to:

Draft