read

If you recently installed Grav in your Cpanel hosting, what could happen is that you notice your content is accessible with both the non-www URL and the www URL.

This has the consequence that your website might be penalized at the search engine of your preference since the search engine will not be able to determine reliably what’s the canonical URL for your content.

Step 1 - Set your base URL

This is what sets the <link rel="canonical" /> tag to the URL you specify. Don’t use the trailing slash, as it’s already set for the routes this URL is concatenated with.

If you have the Admin plugin, this is as easy as going to Configuration > Advanced > Custom base URL

Custom base URL at Grav Admin

You might also want to enable Absolute URLs, but depending on your plugins and themes, this might break functionality. During our tests, it broke pagination over a theme in a test site. Our recommendation is to check the source code of the resulting HTML and check the headers and links to see if there aren’t any malformed URLs. This is an example of a malformed URL we found: https://www.example.comhttps://www.example.com/blog…

The Absolute URLs setting is found as well in Configuration > Advanced > Absolute URLs when using the Admin plugin.

Absolute URLs at Grav Admin

Using the Admin plugin is the preferred way, as you might not be certain which configuration file your Grav is running on. In my case, I want to look for the custom_base_url and absolute_urls in the YAML files within the user directory and the results were the following:

root@server [~/public_html/user]# grep -rn "custom_base_url" .
./config/system.yaml:9:custom_base_url: null
./www.example.com/config/system.yaml:8:custom_base_url: 'https://www.example.com'
root@server [~/public_html/user]#

The above command is a recursive text search showing the line number of findings in files. The dot at the end is the directory where to start searching.

This will set the <link rel="canonical" /> tag on every page and search engines will know the source of truth of your content.

Step 2 - Force redirection with mod_rewrite in your Web Server (optional)

You might want to take a step further by adding a redirect to make sure your links are only found in one source. This is done with the mod_rewrite plugin of the Apache webserver or the Lightspeed Web Server most cPanel installations include.

If you see the stock .htaccess Grav includes, you will notice there are 6 sections:

  • RewriteBase
  • X-Forwarded-Proto
  • Exploits
  • Index
  • Security
  • Prevent Browsing and Set Default Resources

We want to include our RewriteCond and RewriteRule after the end section of Exploits, but before the start section of the Index. The Rewrite rules run from top to down, and the order and placement of the rules are important. Otherwise, we risk our site being made vulnerable, making our rules ignored because of one taking precedence before ours, or breaking the website entirely.

www-redirect at lines 46 and 47

Using www redirection is good for your website, check out the Yes WWW website for more information.

If you want to make a non-www redirection to the www version of your site, the code to add is:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ "https\:\/\/www\.example\.com\/$1" [R=301,L]

If you want to make a www redirection to the non-www version of your site, the code is similar:

RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
RewriteRule ^(.*)$ "https\:\/\/example\.com\/$1" [R=301,L]

Be careful at replacing the domain with yours, but without deleting any of the other characters, as it will break the functionality of this patch.

There's a third step you might want to explore at: GRAV Duplicate Data? - NickSchick | Freelance Design Concepts

This blog post is courtesy from the forum question: How do I force canonical URL in Grav?

Blog Logo

Ivan Montilla Miralles


Published

Image

Frontier Themes

Back to Overview