diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py index 87ab0819082..7cf4d5fd1f8 100644 --- a/addons/website/controllers/main.py +++ b/addons/website/controllers/main.py @@ -28,6 +28,7 @@ from odoo.addons.http_routing.models.ir_http import slug, slugify, _guess_mimety from odoo.addons.portal.controllers.portal import pager as portal_pager from odoo.addons.portal.controllers.web import Home from odoo.addons.web.controllers.binary import Binary +from odoo.addons.website.tools import get_base_domain logger = logging.getLogger(__name__) @@ -141,7 +142,7 @@ class Website(Home): if not isredir and website.domain: domain_from = request.httprequest.environ.get('HTTP_HOST', '') - domain_to = werkzeug.urls.url_parse(website.domain).netloc + domain_to = get_base_domain(website.domain) if domain_from != domain_to: # redirect to correct domain for a correct routing map url_to = werkzeug.urls.url_join(website.domain, '/website/force/%s?isredir=1&path=%s' % (website.id, path)) diff --git a/addons/website/models/website.py b/addons/website/models/website.py index 06df281684f..02bb9237ac0 100644 --- a/addons/website/models/website.py +++ b/addons/website/models/website.py @@ -19,7 +19,7 @@ from markupsafe import Markup from odoo import api, fields, models, tools, http, release, registry from odoo.addons.http_routing.models.ir_http import RequestUID, slugify, url_for from odoo.addons.website.models.ir_http import sitemap_qs2dom -from odoo.addons.website.tools import similarity_score, text_from_html +from odoo.addons.website.tools import similarity_score, text_from_html, get_base_domain from odoo.addons.portal.controllers.portal import pager from odoo.addons.iap.tools import iap_tools from odoo.exceptions import AccessError, MissingError, UserError, ValidationError @@ -312,6 +312,20 @@ class Website(models.Model): configurator_action_todo = self.env.ref('website.website_configurator_todo') return configurator_action_todo.action_launch() + def _is_indexable_url(self, url): + """ + Returns True if the given url has to be indexed by search engines. + It is considered that the website must be indexed if the domain name + matches the URL. We check if they are equal while ignoring the www. and + http(s). This is to index the site even if the user put the www. in the + settings while he has a configuration that redirects the www. to the + naked domain for example (same thing for http and https). + + :param url: the url to check + :return: True if the url has to be indexed, False otherwise + """ + return get_base_domain(url, True) == get_base_domain(self.domain, True) + # ---------------------------------------------------------- # Configurator # ---------------------------------------------------------- @@ -971,7 +985,7 @@ class Website(models.Model): def _filter_domain(website, domain_name, ignore_port=False): """Ignore `scheme` from the `domain`, just match the `netloc` which is host:port in the version of `url_parse` we use.""" - website_domain = urls.url_parse(website.domain or '').netloc + website_domain = get_base_domain(website.domain) if ignore_port: website_domain = _remove_port(website_domain) domain_name = _remove_port(domain_name) diff --git a/addons/website/tools.py b/addons/website/tools.py index 4f40c38aee7..afe8eff2109 100644 --- a/addons/website/tools.py +++ b/addons/website/tools.py @@ -1,6 +1,7 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. import contextlib import re +import werkzeug.urls from lxml import etree from unittest.mock import Mock, MagicMock, patch @@ -167,3 +168,21 @@ def text_from_html(html_fragment, collapse_whitespace=False): if collapse_whitespace: content = re.sub('\\s+', ' ', content).strip() return content + +def get_base_domain(url, strip_www=False): + """ + Returns the domain of a given url without the scheme and the www. and the + final '/' if any. + + :param url: url from which the domain must be extracted + :param strip_www: if True, strip the www. from the domain + + :return: domain of the url + """ + if not url: + return '' + + url = werkzeug.urls.url_parse(url).netloc + if strip_www and url.startswith('www.'): + url = url[4:] + return url diff --git a/addons/website/views/website_templates.xml b/addons/website/views/website_templates.xml index 575093d3154..77ab0c0ee26 100644 --- a/addons/website/views/website_templates.xml +++ b/addons/website/views/website_templates.xml @@ -91,7 +91,7 @@ + and not main_object.website_indexed) or (website.domain and not website._is_indexable_url(request.httprequest.url_root))" name="robots" content="noindex"/> @@ -2390,7 +2390,7 @@