diff --git a/lib/Baser/Test/Case/View/Helper/BcBaserHelperTest.php b/lib/Baser/Test/Case/View/Helper/BcBaserHelperTest.php
index bc807197c0..34381bb2a3 100644
--- a/lib/Baser/Test/Case/View/Helper/BcBaserHelperTest.php
+++ b/lib/Baser/Test/Case/View/Helper/BcBaserHelperTest.php
@@ -2381,8 +2381,11 @@ public function setAlternateUrlDataProvider()
* testSetAlternateUrl
* @dataProvider setCanonicalUrlDataProvider
*/
- public function testSetCanonicalUrl($url, $expected)
+ public function testSetCanonicalUrl($url, $expected, $canonicalUrl = null)
{
+ if (!is_null($canonicalUrl)) {
+ $this->_View->set('canonicalUrl', $canonicalUrl);
+ }
Configure::write('BcSite.use_site_device_setting', true);
$this->BcBaser->request = $this->_getRequest($url);
$this->BcBaser->setCanonicalUrl();
@@ -2396,6 +2399,8 @@ public function setCanonicalUrlDataProvider()
['/index.html', ''],
['/about/index.html', ''],
['/s/', ''],
+ ['/', '', 'https://example.com'],
+ ['/', '', false],
];
}
}
diff --git a/lib/Baser/View/Helper/BcBaserHelper.php b/lib/Baser/View/Helper/BcBaserHelper.php
index 43bea746e1..f7bf2cdc57 100755
--- a/lib/Baser/View/Helper/BcBaserHelper.php
+++ b/lib/Baser/View/Helper/BcBaserHelper.php
@@ -2842,6 +2842,23 @@ public function afterRender($viewFile)
* - PCサイトが存在する場合、canonicalを出力
*/
public function setCanonicalUrl() {
+ $canonicalUrl = $this->_View->get('canonicalUrl');
+ if ($canonicalUrl === false) {
+ return;
+ }
+ if ($canonicalUrl) {
+ return $this->_View->set('meta',
+ $this->BcHtml->meta('canonical',
+ $this->BcHtml->url($canonicalUrl, true),
+ [
+ 'rel' => 'canonical',
+ 'type' => null,
+ 'title' => null,
+ 'inline' => false
+ ]
+ )
+ );
+ }
$currentSite = BcSite::findCurrent();
if (!$currentSite) {
return;