diff --git a/config.toml b/config.toml
index a5e851e..7f1ed17 100644
--- a/config.toml
+++ b/config.toml
@@ -68,6 +68,12 @@ browser_theme_color = "#087e96"
# You can load a stylesheet for a single post by adding it to the [extra] section of the post's front matter, following this same format.
stylesheets = []
+# Sets the default canonical URL for all pages.
+# Individual pages can override this in the [extra] section using canonical_url.
+# Example: "$base_url/blog/post1" will get the canonical URL "https://example.com/blog/post1".
+# Note: To ensure accuracy in terms of matching content, consider setting 'canonical_url' individually per page.
+# base_canonical_url = "https://example.com"
+
# Remote repository for your Zola site.
# Used for `show_remote_changes` and `show_remote_source` (see below).
# Supports GitHub, GitLab, Gitea, and Codeberg.
diff --git a/templates/partials/header.html b/templates/partials/header.html
index b969e75..fbbd630 100644
--- a/templates/partials/header.html
+++ b/templates/partials/header.html
@@ -82,13 +82,6 @@
- {# Add og:locale and hreflang tags for multilingual sites #}
- {%- if config.languages | length > 0 and current_url %}
- {%- include "partials/multilingual_tags.html" -%}
- {%- else -%}
-
- {%- endif %}
-
{# Image for social media sharing #}
{%- set social_media_card = macros_settings::evaluate_setting_priority(setting="social_media_card", page=page | default(value=""), section=section | default(value=""), default_global_value="") -%}
{% if social_media_card %}
@@ -121,8 +114,28 @@
{% endif %}
+ {# Add og:locale and hreflang tags for multilingual sites #}
+ {%- if config.languages | length > 0 and current_url %}
+ {%- include "partials/multilingual_tags.html" -%}
+ {%- else -%}
+
+ {%- endif %}
+
+ {# Set canonical URL #}
{%- if current_url -%}
-
+ {%- if page.extra.canonical_url or section.extra.canonical_url -%}
+ {%- set canonical_url = page.extra.canonical_url | default(value=section.extra.canonical_url) -%}
+ {%- elif config.extra.base_canonical_url -%}
+ {%- set canonical_url = current_url | replace(from=config.base_url, to=config.extra.base_canonical_url) -%}
+ {%- endif -%}
+ {%- endif -%}
+
+ {# Add canonical URL, if set #}
+ {%- if canonical_url -%}
+
+
+ {%- elif current_url -%}
+
{%- endif -%}
diff --git a/theme.toml b/theme.toml
index df283a3..b22f811 100644
--- a/theme.toml
+++ b/theme.toml
@@ -51,6 +51,12 @@ skin = ""
# You can load a stylesheet for a single post by adding it to the [extra] section of the post's front matter, following this same format.
stylesheets = []
+# Sets the default canonical URL for all pages.
+# Individual pages can override this in the [extra] section using canonical_url.
+# Example: "$base_url/blog/post1" will get the canonical URL "https://example.com/blog/post1".
+# Note: To ensure accuracy in terms of matching content, consider setting 'canonical_url' individually per page.
+# base_canonical_url = "https://example.com"
+
# Remote repository for your Zola site.
# Used for `show_remote_changes` and `show_remote_source` (see below).
# Supports GitHub, GitLab, Gitea, and Codeberg.