/**
* @param CanonicalUrlEvent $event
*/
public function generateUrlCanonical(CanonicalUrlEvent $event)
{
if ($event->getUrl() !== null) {
return;
}
$parseUrlByCurrentLocale = $this->getParsedUrlByCurrentLocale();
if (empty($parseUrlByCurrentLocale['host'])) {
return;
}
// Be sure to use the proper domain name
$canonicalUrl = $parseUrlByCurrentLocale['scheme'] . '://' . $parseUrlByCurrentLocale['host'];
// preserving a potential subdirectory, e.g. http://somehost.com/mydir/index.php/...
$canonicalUrl .= $this->request->getBaseUrl();
// Remove script name from path, e.g. http://somehost.com/index.php/...
$canonicalUrl = preg_replace("!/index(_dev)?\\.php!", '', $canonicalUrl);
$path = $this->request->getPathInfo();
if (!empty($path) && $path != "/") {
$canonicalUrl .= $path;
$canonicalUrl = rtrim($canonicalUrl, '/');
} else {
$queryString = $this->request->getQueryString();
if (!empty($queryString)) {
$canonicalUrl .= '/?' . $queryString;
}
}
$event->setUrl($canonicalUrl);
}