• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Engaged ,
Mar 31, 2019 Mar 31, 2019

Copy link to clipboard

Copied

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!

Views

622

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 01, 2019 Apr 01, 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_

...

Votes

Translate

Translate
Community Expert ,
Mar 31, 2019 Mar 31, 2019

Copy link to clipboard

Copied

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 & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2019 Apr 01, 2019

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines