Skip to content

Commit

Permalink
Merge pull request #1 from roadster31/readme
Browse files Browse the repository at this point in the history
Improved URL generation
  • Loading branch information
gillesbourgeat committed Apr 3, 2016
2 parents 7d4745f + 4e6cab1 commit 9115353
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 51 deletions.
8 changes: 7 additions & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.2</version>
<version>1.0.3</version>
<authors>
<author>
<name>Gilles Bourgeat</name>
<email>gbourgeat@openstudio.fr</email>
<website>https://github.com/thelia-modules/CanonicalUrl</website>
</author>
<author>
<name>Franck Allimant</name>
<company>CQFDev</company>
<email>franck@cqfdev.fr</email>
<website>www.cqfdev.fr</website>
</author>
</authors>
<type>classic</type>
<thelia>2.1.0</thelia>
Expand Down
67 changes: 42 additions & 25 deletions EventListener/CanonicalUrlListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@

namespace CanonicalUrl\EventListener;

use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Model\ConfigQuery;
use Thelia\Model\LangQuery;
use CanonicalUrl\Event\CanonicalUrlEvent;
use CanonicalUrl\Event\CanonicalUrlEvents;
use Thelia\Model\RewritingUrl;
use Thelia\Model\RewritingUrlQuery;

/**
* Class CanonicalUrlListener
Expand Down Expand Up @@ -48,35 +51,49 @@ public function __construct(Request $request)
*/
public function generateUrlCanonical(CanonicalUrlEvent $event)
{
$preUrl = null;
$url = null;
$addUrlParameters = true;

$uri = $this->request->getUri();
$parseUrlByCurrentLocale = $this->getParsedUrlByCurrentLocale();

// Be sure to use the proper domain name
$canonicalUrl = $parseUrlByCurrentLocale['scheme'] . '://' . $parseUrlByCurrentLocale['host'];

$parseUrlByCurrentLocale = $this->getParseUrlByCurrentLocale();
$uri = $this->request->getUri();

if (!empty($uri) && false !== $parse = parse_url($uri)) {
// test if current domain equal lang domain
if ($parse['host'] !== $parseUrlByCurrentLocale['host']) {
$preUrl = $parseUrlByCurrentLocale['scheme'] . '://' . $parseUrlByCurrentLocale['host'];
}
// Remove script name from path, preserving a potential subdirectory, e.g. http://somehost.com/mydir/index.php/...
$filePart = preg_replace("!/index(_dev)?\.php!", '', $parse['path']);

$canonicalUrl .= $filePart;

// If URL rewriting is enabled, check if our URL is rewritten.
// If it's the case, we will not add parameters to prevent duplicate content.
if (ConfigQuery::isRewritingEnable()) {
$pathList = [];

$filePart = trim($filePart, '/');

if (strpos($parse['path'], '/index.php') > -1) {
$path = explode('/index.php', $parse['path']);
$url = $path[1];
} elseif (strpos($parse['path'], '/index_dev.php') > -1) {
$path = explode('/index_dev.php', $parse['path']);
$url = $path[1];
} elseif ($parse['path'] !== '/') {
$url = $parse['path'];
while (! empty($filePart)) {
$pathList[] = $filePart;

$filePart = preg_replace("!^[^/]+/?!", '', $filePart);
}

// Check if we have a rewriten URL
$addUrlParameters = 0 === RewritingUrlQuery::create()->filterByUrl($pathList, Criteria::IN)->count();
}
}

if (empty($url)) {
$url = '/?' . $this->request->getQueryString();
if ($addUrlParameters) {
$queryString = $this->request->getQueryString();

if (! empty($queryString)) {
$canonicalUrl .= '?' . $queryString;
}

}

$event->setUrl($preUrl . $url);
$event->setUrl($canonicalUrl);
}

/**
Expand Down Expand Up @@ -106,26 +123,26 @@ public static function getSubscribedEvents()
* query - after the question mark ?
* fragment - after the hashmark #
*/
protected function getParseUrlByCurrentLocale()
protected function getParsedUrlByCurrentLocale()
{
// for one domain by lang
if ((int) ConfigQuery::read('one_domain_foreach_lang', 0) === 1) {
// $langUrl = $this->session->getLang()->getUrl();

$langUrl = LangQuery::create()->findOneByLocale($this->session->getLang()->getLocale())->getUrl();
// We always query the DB here, as the Lang configuration (then the related URL) may change during the
// user session lifetime, and improper URLs could be generated. This is quite odd, okay, but may happen.
$langUrl = LangQuery::create()->findPk($this->request->getSession()->getLang()->getId())->getUrl();

if (!empty($langUrl) && false !== $parse = parse_url($langUrl)) {
return $parse;
}
}

// return config url site
// Configured site URL
$urlSite = ConfigQuery::read('url_site');
if (!empty($urlSite) && false !== $parse = parse_url($urlSite)) {
return $parse;
}

// return current host
// return current URL
return parse_url($this->request->getUri());
}
}
50 changes: 25 additions & 25 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
# Canonical Url

- This module generates the canonical.

- If the url is not rewrite, the canonical url contains the "$_GET" parameters

Example for : For url ```http://demo.thelia.net/?view=product&locale=en_US&product_id=18```
```html
<link rel="canonical" href="/?view=product&locale=en_US&product_id=18" />
```

- If you have "index.php" in your URL, it will be deleted
Example for : For url ```http://demo.thelia.net/index.php?view=product&locale=en_US&product_id=18```
```html
<link rel="canonical" href="/?view=product&locale=en_US&product_id=18" />
```
Example for : For url ```http://demo.thelia.net/index.php/en_en-your-path.html?page=44```
```html
<link rel="canonical" href="/en_en-your-path" />
```

- If the user comes from the bad domain, the canonical URL contains the good domain
Example for : For url ```http://demo458.thelia.net/index.php/en_en-your-path.html?page=44```
```html
<link rel="canonical" href="http://demo.thelia.net/en_en-your-path" />
```
This module generates a canonical URL for every page of your shop. Once activated, you'll find a `<link rel="canonical" href="..." />` tag in the header of your pages.

## Examples

- If the page URL is not rewritten, the canonical URL will contain all the URL parameters. Example for : For URL ```http://demo.thelia.net/?view=product&locale=en_US&product_id=18```
```html
<link rel="canonical" href="http://demo.thelia.net/?view=product&locale=en_US&product_id=18" />
```
Obviously, this is far from ideal. Consider activating URL rewriting !

- When the page URL contains the script name (index.php), it will be removed from the canonical URL. Example, the canonical URL of ```http://demo.thelia.net/index.php?view=product&locale=en_US&product_id=18``` is :
```html
<link rel="canonical" href="http://demo.thelia.net/?view=product&locale=en_US&product_id=18" />
```
When a rewritten URL contains parameters, these parameters a removed. For ```http://demo.thelia.net/index.php/en_en-your-path.html?page=44```, the canonical URL is :
```html
<link rel="canonical" href="http://demo.thelia.net/en_en-your-path" />
```

- If the page URL contains a domain which is not the main shop domain, this domain is replaced by the main shop domain. For ```http://demo458.thelia.net/index.php/en_en-your-path.html?page=44``` the canonical URL is :
```html
<link rel="canonical" href="http://demo.thelia.net/en_en-your-path" />
```

## Installation

Expand All @@ -42,4 +42,4 @@ composer require thelia/canonical-url-module:~1.0.0
## Usage
You just have to activate the module and check the metas of your website.
You just have to activate the module and check the meta tags of your shop.

0 comments on commit 9115353

Please sign in to comment.