Skip to content

Commit

Permalink
[8.0][IMP][website_canonical_url] Safer, prefer root, relative paths (O…
Browse files Browse the repository at this point in the history
…CA#260)

[IMP] website_canoncial_url: Several improvements

* Use safe method to retrieve lang.

This avoids a possible `KeyError`.

* Use relative paths.

Makes this work fine behind an HTTPS proxy.

* Prefer `/` over `/page/homepage`.

The root path canonicalized is more SEO and user friendly.
  • Loading branch information
yajo committed Dec 19, 2016
1 parent 006e790 commit 88fba30
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions website_canonical_url/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Contributors

* Thomas Rehn <[email protected]>
* Rami Alwafaie <[email protected]>
* Jairo Llopis <[email protected]>

Maintainer
----------
Expand Down
8 changes: 3 additions & 5 deletions website_canonical_url/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

{
'name': "Website Canoncial URL",
'summary': """
Canonical URL in Website Headers
""",
'author': "initOS GmbH, Odoo Community Association (OCA)",
'summary': "Canonical URL in Website Headers",
'author': "initOS GmbH, Tecnativa, Odoo Community Association (OCA)",
'website': "http://www.initos.com",
'category': 'Website',
'version': '8.0.1.0.0',
'version': '8.0.1.1.0',
'license': 'AGPL-3',
'depends': [
'website',
Expand Down
15 changes: 11 additions & 4 deletions website_canonical_url/models/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ def get_canonical_url(self, req=None):
canonical_url = None
if req is None:
req = request
if req and req.httprequest.base_url:
lang = self._context['lang']
if req and req.httprequest.path:
lang = self.env.lang
if lang == request.website.default_lang_code:
canonical_url = req.httprequest.base_url
canonical_url = req.httprequest.path
else:
url_parts = urlparse(req.httprequest.base_url)
url_parts = urlparse(req.httprequest.path)
url_parts = list(url_parts)
# change the path of the url
url_parts[2] = lang + url_parts[2]
canonical_url = urlunparse(url_parts)
# Special case for rerouted requests to root path
try:
if (canonical_url.startswith("/page/") and
self.menu_id.child_id[0].url == canonical_url):
canonical_url = "/"
except:
pass
return canonical_url

0 comments on commit 88fba30

Please sign in to comment.