Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-05-19 16:17:07

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,370
GitHub

Using tags to populate <link rel="canonical"> - more efficient way?

Hi.
I’ve got a line of code that I’m quite pleased with:

<link rel="canonical" href="<txp:if_individual_article><txp:permlink /><txp:else /><txp:if_section name=",default"><txp:site_url /><txp:else /><txp:site_url /><txp:section /></txp:if_section></txp:if_individual_article>" />

This, if my observations are correct, will correctly spit out the absolute URL for i) the site root, ii) a section and iii) an individual article. Ignoring search results for now, is this the most efficient way of doing it? I came up with after a fair amount of experimenting, so it’s potentially crufty.

Any and all suggestions for efficiency gains gratefully received.

Thanks :)

Pete

Offline

#2 2009-05-19 18:01:56

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,197
Website GitHub Mastodon Twitter

Re: Using tags to populate <link rel="canonical"> - more efficient way?

It looks like it should work. The only thing I’m not sure about is if you actually need the , before the default.


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#3 2009-05-19 19:04:04

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,370
GitHub

Re: Using tags to populate <link rel="canonical"> - more efficient way?

Thanks, colak. I have always used ,default to refer to the root of the site, I’ll try it with and without and report back for future reference.

Offline

#4 2009-05-19 19:16:56

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,370
GitHub

Re: Using tags to populate <link rel="canonical"> - more efficient way?

OK, good news: both of these options will automatically (and correctly) populate the <link rel="canonical href="..." /> section of <head>:

<link rel="canonical" href="<txp:if_individual_article><txp:permlink /><txp:else /><txp:if_section name=",default"><txp:site_url /><txp:else /><txp:site_url /><txp:section /></txp:if_section></txp:if_individual_article>" />

<link rel="canonical" href="<txp:if_individual_article><txp:permlink /><txp:else /><txp:if_section name="default"><txp:site_url /><txp:else /><txp:site_url /><txp:section /></txp:if_section></txp:if_individual_article>" />

More information on rel="canonical" and why it’s a factor in site SEO:

http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=139394
http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html

Offline

#5 2009-05-19 19:40:10

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Using tags to populate <link rel="canonical"> - more efficient way?

name="default" and name="" both point to the front page as far as I know, so name=",default" is just entering the same value twice.

Offline

#6 2009-05-19 20:21:11

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,370
GitHub

Re: Using tags to populate <link rel="canonical"> - more efficient way?

Els wrote:

name="default" and name="" both point to the front page as far as I know, so name=",default" is just entering the same value twice.

That was also my understanding, rather that ,default covers both bases (no name and default) and is still semantically correct.

Offline

#7 2009-08-28 15:45:41

beechy
Member
Registered: 2006-03-02
Posts: 92

Re: Using tags to populate <link rel="canonical"> - more efficient way?

Hi gaekwad,

This is awsome, any ideas how I can get the category and author pages to display the rel=“canonical” link as well – at the moment they just display my site url as both are handled by my default page.

Last edited by beechy (2009-08-28 15:46:54)

Offline

#8 2010-05-25 12:37:20

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,370
GitHub

Re: Using tags to populate <link rel="canonical"> - more efficient way?

beechy wrote:

Hi gaekwad,
This is awsome, any ideas how I can get the category and author pages to display the rel=“canonical” link as well – at the moment they just display my site url as both are handled by my default page.

I’ll take a look – hadn’t thought of that :) Keep you posted.

Offline

#9 2010-05-25 21:12:43

merz1
Member
From: Hamburg
Registered: 2006-05-04
Posts: 994
Website

Re: Using tags to populate <link rel="canonical"> - more efficient way?

Alternative (?): Please also check/consider/compare smd_short_url: tiny/canonical URLs

Note: I am not using ‘smd_short_url: tiny/canonical URLs’ so I am not able to compare/check the output. And yes gaekwad, a zoo of if/else conditions can cover all possible canonical URL conditions :)


Get all online mentions of Textpattern via OPML subscription: TXP Info Sources: Textpattern RSS feeds as dynamic OPML

Offline

Board footer

Powered by FluxBB