Solved

SEO - dynamically setting the canonical link

Posted on 2011-02-20
2
697 Views
Last Modified: 2013-11-19
Hi,

I'm for some advice on how to implement a link tag in the head of my page with the "rel=canonical" setting.


the proper syntax for this link is:
       '<link rel="canonical" href="'http://www.mysite.com/my_page'"/>');

The problem that i have is that I'm using a CMS (Netsuite) and i want to set the href value dynamcailly to use the current URL. I added some javascript to do this (see below), but when i view the source, it shows the javascript, instead of the proper tag. Is there a way to set this dynamically?

<script language=javascript> 
	document.writeln('<link rel="canonical" href="' + location.href + '"/>'); 
</script>

Open in new window

0
Comment
Question by:jrattinger
2 Comments
 
LVL 20

Accepted Solution

by:
Proculopsis earned 250 total points
ID: 34941024

You will need to render this statically or server-side for spiders to make any sense of it:

  <link rel="canonical" href="<%=preferredPage%>" />

I'm not too sure why you would want to substitute the same page, as this is used to specify an alternate.
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 250 total points
ID: 34941025
Probably not.
Set it on the server - and you have too many quotes

In PHP:

<link rel="canonical" href="<?PHP echo "http" . ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; ?>"/>
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question