Solved

SEO - dynamically setting the canonical link

Posted on 2011-02-20
2
679 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now