Skip to content

Commit

Permalink
add canonical readme. check hardlink context before removing canonica…
Browse files Browse the repository at this point in the history
…l link
  • Loading branch information
solverat committed Oct 1, 2017
1 parent c0b620a commit efa0189
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ This Bundle helps you mastering this challenges and gives you freedom to elabora
- [Custom Adapter](docs/50_CustomAdapter.md): Learn how to create a custom adapter.
- [Code Examples](docs/60_CodeExamples.md): See some examples.
- [Context Switch Event](docs/70_ContextSwitch.md): Detect Zone/Language/Country Switches.
- [Canonical Links](docs/80_CanonicalLinks.md): Canonical Links in Hardlinks.

## Copyright and License
Copyright: [DACHCOM.DIGITAL](http://dachcom-digital.ch)
Expand Down
15 changes: 11 additions & 4 deletions docs/27_Countries.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# Countries
# Country
This is only required if you're using i18n with a country adapter.
There are two ways to implement a country page like `en-us`:

1. Copy and paste the `en` document tree and rename `en` to `en-us`.
2. Create a hardlink, call it `en-us` and set the reference to `en`.

If you're using a hardlink, check out the [canonical information part](80_CanonicalLinks.md).

## Preparation
Localize your [documents first](26_Languages.md).

## Country Wrapper
If you're using a hardlink for country specific data, create it. Otherwise just create a document as you would normally do.
If you're using a hardlink for country specific data, create it.
Otherwise just create a document as you would normally do.

1. Create a document property (dropdown or text) and call it `country`.
2. Apply the property to all your global pages (like /de, /en) and set the value to `GLOBAL`
3. Apply the property to all your real website hardlinks/documents and set the desired country ISO-Code (uppercase)
2. Apply the property to all your global pages (like `/de`, `/en`) and set the value to `GLOBAL`
3. Apply the property to all your real website hardlinks/documents and set the desired country ISO-Code (uppercase)
3 changes: 2 additions & 1 deletion docs/30_FrontPageMapping.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Front Page Mapping

Sometimes you want to clone a page and expand it with some country information. For example:

```text
Expand All @@ -9,6 +8,8 @@ Sometimes you want to clone a page and expand it with some country information.
- /en-us -> that's your hardlink with a en_US locale.
```

> Note: Learn how to implement a country site based on hardlinks by [clicking here](27_Countries.md).
Now you have a brand new shiny website. If you open the `/en-us/team` page, it will automatically load the content from `/en/team`. That's nice.
If you want to override the team content you just need to copy your `/en/team` into the `/en-us/` tree. Now you're good to go to add some additional content.

Expand Down
36 changes: 36 additions & 0 deletions docs/80_CanonicalLinks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Canonical Links
If you're using hardlinks as country wrapper, there is something you need to know:
Pimcore automatically adds a canonical link to each request header, if a page is wrapped into a hardlink.

## Explanation

```markdown
Document: `/en/news`
HardLink: `/en-us` (source path would be `/en`)
```

Since the `news` document only exists in `/en`, pimcore adds a canonical link to avoid duplicate content - which is perfectly fine.
So if you're going to check the page `/en-us/news`, there should be a canonical relation like this:

```markdown
HTTP/2 200
link: <https://pimcore5-domain4.dev/en/news>; rel="canonical"
```

But in this case, we want to remove the canonical link because we need country specific content and we'll tell search engines about the references via [href-lang tags](25_HrefLang.md) anyway.
So the i18nBundle will remove this canonical tag automatically for your. If you check your http request after installing this bundle, the canonical link will be gone.

## Oh no!
**But!** What if you have some hardlinks inside the country site? For Example:

```markdown
Document: `/en-us/products/demo`
HardLink: `/en-us/special-products` (source path would be `/en-us/products`)
```

Don't worry. Just check the header for `/en-us/special-products/demo`, the canonical relation is still available. The I18nBundle only checks hardlinks on root (`/`), since that's the only place where country based hardlinks makes sense.

```markdown
HTTP/2 200
link: <https://pimcore5-domain4.dev/en-us/products/demo>; rel="canonical"
```
12 changes: 8 additions & 4 deletions src/I18nBundle/EventListener/CanonicalListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ public static function getSubscribedEvents()
];
}

/**
* @param FilterResponseEvent $event
*/
public function onKernelResponse(FilterResponseEvent $event)
{
$request = $event->getRequest();

if (!$event->isMasterRequest()) {
return;
}

$request = $event->getRequest();
if (!$this->matchesPimcoreContext($request, PimcoreContextResolver::CONTEXT_DEFAULT)) {
return;
}
Expand All @@ -55,9 +57,11 @@ public function onKernelResponse(FilterResponseEvent $event)
return;
}

//@todo: check if it's a real i18n hardlink element.
if ($document instanceof WrapperInterface && !Staticroute::getCurrentRoute()) {
$event->getResponse()->headers->remove('Link');
//only remove canonical link, if hardlink source is the country wrapper:
if($document->getHardLinkSource()->getPath() === '/') {
$event->getResponse()->headers->remove('Link');
}
}
}
}
2 changes: 1 addition & 1 deletion src/I18nBundle/Resources/config/services/finder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ services:
autoconfigure: true
public: false

# finder: path check if current path is in right (hard link) context
# finder: path check if current path is in right (hardlink) context
I18nBundle\Finder\PathFinder: ~

0 comments on commit efa0189

Please sign in to comment.