Skip to content

Instantly share code, notes, and snippets.

@allybee
Last active June 19, 2018 10:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allybee/310761098b945b42b995 to your computer and use it in GitHub Desktop.
Save allybee/310761098b945b42b995 to your computer and use it in GitHub Desktop.
Redirect to canonical URLs and add the canonical tag in concrete5 v5.7 (including /index.php?cID=123 format)
<?php // application/bootstrap/app.php
/**
* Redirect to Canonical URLs
*/
// Setup the CanonicalUrls helper
Core::bind('helper/canonical_urls', function() {
return new \Application\Src\Helpers\CanonicalUrls();
});
// Redirect to the Canonical URL on page view
Events::addListener('on_page_view', function($event) {
\Core::make('helper/canonical_urls')->redirectToCanonicalURL();
});
<?php // application/src/Helpers/CanonicalUrls.php
namespace Application\Src\Helpers;
use Concrete\Core\Routing\Redirect;
use Config;
use Page;
/**
* Class CanonicalUrls
*
* @package Application\Src\Helpers
*/
class CanonicalUrls {
/**
* We can make all the methods non-static, here. The __callStatic magic method will translate them for us,
* so that we can either do CanonicalUrls::someMethod() or $canonicalUrls->someMethod()
*/
public static function __callStatic( $methodName, $arguments ) {
// create a new instance of self
$obj = new static();
// call the method
return call_user_func_array( array( $obj, $methodName ), $arguments );
}
/**
*
* Checks if the current page is using the canonical path
*
* @return boolean
*/
public function isCanonicalURL() {
$c = Page::getCurrentPage();
// Stop if we're not in a page object
if ( !is_object($c) ) {
return;
}
$isCanonicalURL = (strpos($_SERVER["REQUEST_URI"], $c->getCollectionPath()) == false) && (strpos($_SERVER["REQUEST_URI"], "cID=$c->cID") == false);
return (bool) $isCanonicalURL;
}
/**
* Redirects to the canonical URL
*/
public function redirectToCanonicalURL() {
$c = Page::getCurrentPage();
$redirectToCanonicalUrl = \Config::get('concrete.seo.redirect_to_canonical_url');
$canonicalUrl = (string) \URL::to($c);
// Stop if we're already on the canonical URL, in in edit mode, or in the dashboard
if ($this->isCanonicalURL() || $c->isEditMode() || $c->isSystemPage() || $c->getCollectionHandle() == null ) {
return;
}
// Add the canonical tag to the header of the page
$v = \View::getInstance();
$v->addHeaderItem( sprintf('<link rel="canonical" href="%s" />', $canonicalUrl) );
// Redirect to the canonical URL
if ($redirectToCanonicalUrl == true) {
return Redirect::page( $c )->send();
}
}
}
<?php // application/config/concrete.php
return array(
'seo' => array(
'url_rewriting' => true,
'url_rewriting_all' => true,
'redirect_to_canonical_url' => true,
)
);
@patelashishpatel
Copy link

Great Work!!
There is just one thing, I think the condition in isCanonicalURL() Method show read:

        if (is_object($c) && $c->isSystemPage() ) {
            return true;
        }

@allybee
Copy link
Author

allybee commented Nov 24, 2015

@patelashishpatel, What I was intending with that code was to exit/not redirecit if $c was not a page object.

I've updated the gist with some more comments and also added a conditional to exit if we're in the dashboard or trying to edit page defaults.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment