Announcement

  •  » Requests
  •  » Improvements to HTML link rel=canonical

#1 2016-10-29 16:32:06

gnurob
Member
2016-10-29
20

Improvements to HTML link rel=canonical

Hi, everyone:

A canonical link tag is very important for search engines, and page rankings, for a number of reasons. Most importantly, the canonical link signals which version of a page is considered the primary source, and prevents duplicates. It is also helpful in making sure the cleanest URL is used in search results. It's not a difficult subject, and is well explained here: https://yoast.com/rel-canonical/#self-r … -canonical

Recently Google began encouraging the use of HTTPS. Many sites, including my own, now support both HTTP and HTTPS side-by-side. At least transitionally. This does result in duplicate pages. For example:

http://www.robertmiller.ca/gallery/pict … category/1
https://www.robertmiller.ca/gallery/pic … category/1

Force HTTPS, by bonhommedeneige, does a great job of switching users over from HTTP to HTTPS, but there are two problems.

1. The redirect status code is 302 which is a temporary redirect. See for yourself with this handy tool. 302 redirects are not handled well by search engines, and do not pass link juice to the updated URL. (P.S. bonhommedeneige, if you're reading this, a 301/302 config options would be very helpful)

2. The implementation of rel-canonical is broken in Piwigo (this is the main reason for my post)...

Broken?! Yes, the link used by Piwigo is relative and does not include the protocol. It also probably doesn't clean GET variable and in page anchors off of the URL (but I haven't checked). In the example above, both the HTTP and HTTPS versions of the page will declared as canonical. Two identical pages cannot both be canonical.

In my example, the rel=canonical link should change from

<link rel="canonical" href="/gallery/">

to

<link rel="canonical" href="https://www.robertmiller.ca/gallery" />

and be configured as

<link rel="canonical" href="[protocol select][site address][relative URL][and strip any parameters]" />

Piwigo version: 2.8.2
Piwigo URL: https://www.robertmiller.ca/gallery/

Last edited by gnurob (2016-10-29 16:33:12)

Offline

 

#2 2016-10-30 22:15:10

gnurob
Member
2016-10-29
20

Re: Improvements to HTML link rel=canonical

Digging in:

include/functions_url.inc.php has the following code

Code:

/**
 * Returns the 'home page' of this gallery
 */
function get_gallery_home_url()
{
  global $conf;
  if (!empty($conf['gallery_url']))
  {
    if (url_is_remote($conf['gallery_url']) or $conf['gallery_url'][0]=='/' )
    {
      return $conf['gallery_url'];
    }
    return get_root_url().$conf['gallery_url'];
  }
  else
  {
    return make_index_url();
  }
}

and

Code:

function make_index_url($params = array())
{
  global $conf;
  $url = get_root_url().'index';
  if ($conf['php_extension_in_urls'])
  {
    $url .= '.php';
  }
  if ($conf['question_mark_in_urls'])
  {
    $url .= '?';
  }

  $url_before_params = $url;

  $url.= make_section_in_url($params);
  $url = add_well_known_params_in_url($url, $params);

  if ($url == $url_before_params)
  {
    $url = get_absolute_root_url( url_is_remote($url) );
  }

  return $url;
}

and

Code:

/**
 * returns a prefix for each url link on displayed page
 * and return an empty string for current path
 * @return string
 */
function get_root_url()
{
  global $page;
  if ( ($root_url = @$page['root_path']) == null )
  {// TODO - add HERE the possibility to call PWG functions from external scripts
    $root_url = PHPWG_ROOT_PATH;
    if ( strncmp($root_url, './', 2) == 0 )
    {
      return substr($root_url, 2);
    }
  }
  return $root_url;
}

Somewhere around here I ran out of talent and couldn't find a good place to insert this (mostly because link rel=canonical does use a full url anyways):

Code:

// $root_url = preg_replace("/^http:/i", "https:", $root_url);

Offline

 

#3 2016-11-07 14:40:28

WuppiGER
Member
Germany
2016-05-31
38

Re: Improvements to HTML link rel=canonical

my webhoster offer an SSL-Option to redirect every http-Access to https. 301 of course! [ http://galerie.fotobastler.de/ ]

You can make this by your own via htaccess-Rule! What the reason for the force_https-Plugin? I don't need this ...

---

The problem with rel=canonical: not really a problem ... searchengines added the domains ... of course: if you use http and https, there would be an problem... But there is no reason to use http ;) 301 for every http-Access ;)

You can change the path in rel=canonical:

Themes => header.tpl (new template [based on your favorite-template] because new template-updates overwrite your changes)

Search something like this
<link rel="canonical" href="{$U_CANONICAL}" />

change it to:
<link rel="canonical" href="https://yourdomain/{$U_CANONICAL}" />

BEFORE [i have it @ the beginning of the file]

{if $PAGE_TITLE==l10n('Startseite')}
{assign var=U_CANONICAL value=""}
{else if $PAGE_TITLE==l10n('Schlagwörter')}
{assign var=U_CANONICAL value="tags.php"}
{else}{assign var=U_CANONICAL value=$U_CANONICAL}{/if}

Sorry - German Version ... Startseite = Home ? ... Schlagwörter = Tags ?
In the english version should be similiar ... [without l10n?!]

Problem: on the Home ( / ) and on tags.php: the canonical-URL are broken ... you must change this with this code ...

I hope you understand my english ;)

Offline

 

#4 2016-11-07 16:25:30

gnurob
Member
2016-10-29
20

Re: Improvements to HTML link rel=canonical

301 is better than 302, in that a 302 would cause a penalty, because it is really a configuration error if used on a permanent basis, and 301 diminishes the benefits of backlinks. In other words, re-directions are not good for SEO. There's no debate on this, all advice indicates that you should avoid this.

Your advice on fixing header.tpl almost works, but has a couple problems:

1. In some locations, it is necessary to add a trailing slash, and in others it is not, therefore a portion of the canonical links would be broken using either variation. (Ah, I noticed you caught that yourself after writing this.)

2. Not all users are aware of the SEO significance of 301 and 302 re-directs.

3. Those that are aware are not certain on where to make the change.

A menu setting to declare the preferred protocol would be preferable because users can be informed as to why, the fix is easy, and new errors would not be introduced (like the trailing slash problem).

Yes, .htaccess rules, if enabled on the server, allow webmasters to create their own re-directs. However some are limited to editing the server .conf files and may not have permission, and others have limited capacity to learn yet one more form of technology in order to get the job done.

I imagine there are some professional photographers and artists out there that were advanced enough to get their own PHP gallery going and are really depending on good SEO to get noticed. These suggestions should make that goal a little closer for them.

Thanks for offering your suggestions! And your German is far better than mine!

Rob

Offline

 

#5 2017-11-08 00:39:04

petitssuisses
Member
France
2013-05-03
36

Re: Improvements to HTML link rel=canonical

Hi
I've released a new version of the plugin which gives you the option to choose between redirect code value (301 or 302)
Arnaud (bonhomedeneige)

Offline

 
  •  » Requests
  •  » Improvements to HTML link rel=canonical

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact