Highlighted

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

Contributor ,
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!

Adobe Community Professional
Correct answer by Paul-M | Adobe Community Professional

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.

Views

251

Likes

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

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

Contributor ,
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!

Adobe Community Professional
Correct answer by Paul-M | Adobe Community Professional

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.

Views

252

Likes

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
Mar 31, 2019 0
Adobe Community Professional ,
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, ACP
Alt-Web Design & Publishing

Likes

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
Reply
Loading...
Mar 31, 2019 1
Adobe Community Professional ,
Apr 01, 2019

Copy link to clipboard

Copied

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, ACP

Likes

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
Reply
Loading...
Apr 01, 2019 1