diff --git a/addons/website/models/ir_http.py b/addons/website/models/ir_http.py index b758caba434fc..6a675a1245079 100644 --- a/addons/website/models/ir_http.py +++ b/addons/website/models/ir_http.py @@ -247,13 +247,22 @@ def _get_translation_frontend_modules_name(cls): @classmethod def _serve_page(cls): req_page = request.httprequest.path - page_domain = [('url', '=', req_page)] + request.website.website_domain() - published_domain = page_domain + def _search_page(comparator='='): + page_domain = [('url', comparator, req_page)] + request.website.website_domain() + return request.env['website.page'].sudo().search(page_domain, order='website_id asc', limit=1) + # specific page first - page = request.env['website.page'].sudo().search(published_domain, order='website_id asc', limit=1) + page = _search_page() + + # case insensitive search + if not page: + page = _search_page('=ilike') + if page: + logger.info("Page %r not found, redirecting to existing page %r", req_page, page.url) + return request.redirect(page.url) - # redirect withtout trailing / + # redirect without trailing / if not page and req_page != "/" and req_page.endswith("/"): return request.redirect(req_page[:-1]) diff --git a/addons/website/tests/test_page.py b/addons/website/tests/test_page.py index 3f5836bec44b8..9d61075617a50 100644 --- a/addons/website/tests/test_page.py +++ b/addons/website/tests/test_page.py @@ -271,3 +271,10 @@ def test_homepage_not_slash_url(self): root_html = html.fromstring(r.content) canonical_url = root_html.xpath('//link[@rel="canonical"]')[0].attrib['href'] self.assertEqual(canonical_url, website.domain + "/") + + def test_page_url_case_insensitive_match(self): + r = self.url_open('/page_1') + self.assertEqual(r.status_code, 200, "Reaching page URL, common case") + r2 = self.url_open('/Page_1', allow_redirects=False) + self.assertEqual(r2.status_code, 302, "URL exists only in different casing, should redirect to it") + self.assertTrue(r2.headers.get('Location').endswith('/page_1'), "Should redirect /Page_1 to /page_1")