Skip to main content
Inspiring
March 31, 2019
Answered

What's the best syntax to use in rel="canonical" in this context?

  • March 31, 2019
  • 2 replies
  • 1321 views

You guys taught me that it's good practice to use "canonical" to establish a single universal url to a page that can be accessed via multiple urls.

For context, I have this .htaccess code reformatting my urls :

RewriteEngine On

##### FORCE SECURE URL
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

##### REMOVE /PAGES FROM URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/pages/
RewriteRule ^(.*)$ /pages/$1

##### REMOVE .PHP FROM URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

What it does is :

  • Redirects all regular connections to secure ones (http:// to https://)
  • Makes it possible to access pages with or without the location of their folder in the url (/pages)
  • Makes it possible to access pages with or without the file extension in the url (.php)

(Additionally, since I don't usually include "www" in my advertising, I favor the non-www version of the url over the one with it.)

As a result from all that, every page can be accessed at least half a dozen different ways.

Ideally, I'd like "domain.com/page" to be the recognized format.

The shorter the better, IMO.

But I'm still not sure which of these 3 syntaxes would be best to include in the rel="canonical" link (or if the 3rd one would even work) :

In a perfect world, all links would lead to "domain.com/page" (in secure mode). I'm just not 100% sure of the syntax to get there. In fact, if I knew how to rewrite that .htaccess code to redirect to the correct syntax of the url (instead of allowing all versions of the url to work, as it currently does) I just might.

Thanks in advance!

This topic has been closed for replies.
Correct answer Paul-M

If you want to use NON www. then this is your canonical tag format

<link rel="canonical" href="https://domain.com/page">

Also in htaccess you can use a combined rule to force all URLs onto https and any www. requests to the equivelant non www.

Replace this

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

With this:

RewriteCond %{HTTPS} off [OR]

RewriteCond %{HTTP_HOST} ^www\. [NC]

RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]

RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

You can also set your preferred domain in Google Search Console (in site settings) to tell Google your preferred format, which in your case, is in the format https://domain.com - *Note you may need to switch the old version of Google Search Console first to do this.

2 replies

Paul-MCorrect answer
Legend
April 1, 2019

If you want to use NON www. then this is your canonical tag format

<link rel="canonical" href="https://domain.com/page">

Also in htaccess you can use a combined rule to force all URLs onto https and any www. requests to the equivelant non www.

Replace this

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

With this:

RewriteCond %{HTTPS} off [OR]

RewriteCond %{HTTP_HOST} ^www\. [NC]

RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]

RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

You can also set your preferred domain in Google Search Console (in site settings) to tell Google your preferred format, which in your case, is in the format https://domain.com - *Note you may need to switch the old version of Google Search Console first to do this.

Paul-M - Community Expert
Nancy OShea
Community Expert
Community Expert
March 31, 2019

According to  Google, you should use the secure protocol and path to the page.

https://example.com/folder/filename.html

Consolidate duplicate URLs - Search Console Help

Nancy O'Shea— Product User & Community Expert