Skip to content

Commit a3e55f8

Browse files
aamantkiatngsreichel
authored
SEO: Add a rel canonical in all CMS pages (#4923)
* SEO: Add a canonical link in all CMS pages * SEO: Add a canonical link in CMS homepage * Added system config * refactor(cms): update canonical URL methods and improve type hints * fix(cms): ensure proper newline at end of Mage_Cms.csv * Fix PHPStand * Fixed Rector --------- Co-authored-by: Ng Kiat Siong <[email protected]> Co-authored-by: Sven Reichel <[email protected]>
1 parent 4ea0ef8 commit a3e55f8

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

app/code/core/Mage/Cms/Block/Page.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ protected function _prepareLayout()
9393
$head->setTitle($page->getTitle());
9494
$head->setKeywords($page->getMetaKeywords());
9595
$head->setDescription($page->getMetaDescription());
96+
97+
// Add canonical tag if enabled
98+
if (Mage::helper('cms')->canUseCanonicalTag()) {
99+
$canonicalUrl = $this->getCanonicalUrl($page);
100+
if ($canonicalUrl) {
101+
$head->addLinkRel('canonical', $canonicalUrl);
102+
}
103+
}
96104
}
97105

98106
return parent::_prepareLayout();
@@ -113,4 +121,40 @@ protected function _toHtml()
113121
$html = $processor->filter($this->getPage()->getContent());
114122
return $this->getMessagesBlock()->toHtml() . $html;
115123
}
124+
125+
/**
126+
* Get canonical URL for CMS page
127+
*/
128+
protected function getCanonicalUrl(Mage_Cms_Model_Page $page): ?string
129+
{
130+
if (!$page->getId()) {
131+
return null;
132+
}
133+
134+
// Check if page is active
135+
if (!$page->getIsActive()) {
136+
return null;
137+
}
138+
139+
// Get the page identifier
140+
$identifier = $page->getIdentifier();
141+
142+
// Handle special pages differently
143+
$homePageId = Mage::getStoreConfig('web/default/cms_home_page');
144+
$noRoutePageId = Mage::getStoreConfig('web/default/cms_no_route');
145+
$noCookiesPageId = Mage::getStoreConfig('web/default/cms_no_cookies');
146+
147+
// For homepage, use base URL
148+
if ($identifier === $homePageId) {
149+
return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
150+
}
151+
152+
// For special pages that shouldn't have canonical tags
153+
if (in_array($identifier, [$noRoutePageId, $noCookiesPageId])) {
154+
return null;
155+
}
156+
157+
// For regular CMS pages, use the standard CMS page URL
158+
return $this->getUrl('', ['_direct' => $identifier, '_nosid' => true]);
159+
}
116160
}

app/code/core/Mage/Cms/Helper/Data.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Mage_Cms_Helper_Data extends Mage_Core_Helper_Abstract
1818
public const XML_NODE_BLOCK_TEMPLATE_FILTER = 'global/cms/block/tempate_filter';
1919
public const XML_NODE_ALLOWED_STREAM_WRAPPERS = 'global/cms/allowed_stream_wrappers';
2020
public const XML_NODE_ALLOWED_MEDIA_EXT_SWF = 'adminhtml/cms/browser/extensions/media_allowed/swf';
21+
public const XML_PATH_USE_CMS_CANONICAL_TAG = 'web/seo/cms_canonical_tag';
2122

2223
protected $_moduleName = 'Mage_Cms';
2324

@@ -68,4 +69,14 @@ public function isSwfDisabled()
6869
{
6970
return true;
7071
}
72+
73+
/**
74+
* Check if <link rel="canonical"> can be used for CMS pages
75+
*
76+
* @param int|string|null|Mage_Core_Model_Store $store
77+
*/
78+
public function canUseCanonicalTag($store = null): bool
79+
{
80+
return Mage::getStoreConfigFlag(self::XML_PATH_USE_CMS_CANONICAL_TAG, $store);
81+
}
7182
}

app/code/core/Mage/Cms/etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@
182182
</global>
183183
<default>
184184
<web>
185+
<seo>
186+
<cms_canonical_tag>1</cms_canonical_tag>
187+
</seo>
185188
<default>
186189
<cms_home_page>home</cms_home_page>
187190
<cms_no_route>no-route</cms_no_route>

app/code/core/Mage/Cms/etc/system.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@
5151
</show_cms_breadcrumbs>
5252
</fields>
5353
</default>
54+
<seo translate="label" module="cms">
55+
<fields>
56+
<cms_canonical_tag translate="label">
57+
<label>Use Canonical Link Meta Tag For CMS Pages</label>
58+
<frontend_type>select</frontend_type>
59+
<source_model>adminhtml/system_config_source_yesno</source_model>
60+
<sort_order>20</sort_order>
61+
<show_in_default>1</show_in_default>
62+
<show_in_website>1</show_in_website>
63+
<show_in_store>1</show_in_store>
64+
</cms_canonical_tag>
65+
</fields>
66+
</seo>
5467
</groups>
5568
</web>
5669
<cms translate="label" module="cms">

app/locale/en_US/Mage_Cms.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"Save Block","Save Block"
9999
"Save Page","Save Page"
100100
"Save Process","Save Process"
101+
"Search Engine Optimization","Search Engine Optimization"
101102
"Select Block...","Select Block..."
102103
"Select Page...","Select Page..."
103104
"Show Breadcrumbs for CMS Pages","Show Breadcrumbs for CMS Pages"
@@ -118,6 +119,7 @@
118119
"This block no longer exists.","This block no longer exists."
119120
"This page no longer exists.","This page no longer exists."
120121
"Title","Title"
122+
"Use Canonical Link Meta Tag For CMS Pages","Use Canonical Link Meta Tag For CMS Pages"
121123
"URL Key","URL Key"
122124
"Unable to find a block to delete.","Unable to find a block to delete."
123125
"Unable to find a page to delete.","Unable to find a page to delete."

0 commit comments

Comments
 (0)