The source file shows the following lines

<link rel="canonical" href="/example-article" />
<link rel="canonical" href="http://www.example.com/example-article" />
<link rel="shortlink" href="/node/1" />

The absolute link is created by the Global Redirect module. The relative one, I guess is a default of Drupal 7 (I am not not sure about this). Google said they recommend absolute links to minimize potential confusion or difficulties.

How can I take off that relative URL?

Comments

jaypan’s picture

Subscribe

Contact me to contract me for D7 -> D10/11 migrations.

jamie_twt’s picture

Hi,
I spent too many hours putting together this solution for canonical links and Drupal 7 so i am glad to share this. It has been an amalgamation of a few different sources of code, however i don't remember where so I'm sorry no references...

function yourthemename_html_head_alter(&$head_elements) {   
unset($head_elements['system_meta_generator']); 
foreach ($head_elements as $key => $element) {     
if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') 
{       
unset($head_elements[$key]);      
}
if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'shortlink') 
{       
unset($head_elements[$key]);      
}   
} 

$urlpath = strtok($_SERVER['REQUEST_URI'],'?');
$can_link = 'http://www.example.com.au'.$urlpath;
$head_elements['canonical'] = array(    
'#type' => 'html_tag',    
'#tag' => 'link',    
'#attributes' => array('rel' => 'canonical',
'href' => $can_link),  
);

}

What i wanted to do was remove, the canonical, shortlink and generator tags. Remove the first unset line and second If statement if you don't want to remove them as well.

To start with I untick the Add Canonical in Global Redirect. Then just add your themename at the top of the function, replace your preferred base url by the $can_link variable depending on what you have setup in Google Analytics or your .htaccess file. Then copy all the code and add it to your template.php file.

Happy to hear any suggestions to improve on this but it works!
Thanks

tecjam’s picture

Thanks for the above solution.

Here was my problem: In the html source code, the meta tags were added only once:

<link rel="canonical" href="/home" />
<link rel="shortlink" href="/node/1" />

Perfect actually.

However, the http header that was sent actually displays the link variable multiple times, so checking my http header on http://network-tools.com gave me the following:

Header are:

HTTP/1.1 200 OK
Date: Tue, 07 Aug 2012 13:02:33 GMT
Server: Apache
X-Drupal-Cache: MISS
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Tue, 07 Aug 2012 13:02:33 +0000
Cache-Control: public, max-age=86400
ETag: "1344344553-0"
Link: </node/1>; rel="shortlink",</home>; rel="canonical",</node/1>; rel="shortlink",</home>; rel="canonical",</node/1>; rel="shortlink",</home>; rel="canonical",</node/1>; rel="shortlink",</home>; rel="canonical",</node/1>; rel="shortlink",</home>; rel="canonical",</node/1>; rel="shortlink",</home>; rel="canonical",</node/1>; rel="shortlink",</home>; rel="canonical",</node/1>; rel="shortlink",</home>; rel="canonical",</node/1>; rel="shortlink",</home>; rel="canonical",</node/1>; rel="shortlink",</home>; rel="canonical",</node/1>; rel="shortlink",</home>; rel="canonical",</home>; rel="canonical",</node/1>; rel="shortlink"
Content-Language: de
Vary: Cookie,Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

As you can see, I have the Link element a lot of times in there and I have no idea where this is coming from - it only happens on the front page.
So I used the above solution to remove all link tags and re-add the canonical link, but how do you go about adding the shortlink as well?