Skip to content

Commit

Permalink
feat: add setting to redirect slugs to lowercase (#7509)
Browse files Browse the repository at this point in the history
This commit adds the REDIRECT_TO_LOWERCASE_SLUG option which will
cause the cms to redirect requests with an non-lowercase slug if
no page with that slug is found.

Implements #1324
  • Loading branch information
pajowu committed Mar 8, 2023
1 parent c7fc2f2 commit 7e852ec
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cms/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.http import Http404
from django.template import Variable
from django.test.utils import override_settings
from django.urls import clear_url_caches
from django.urls import clear_url_caches, reverse

from cms.api import create_page, create_title, publish_page
from cms.models import PagePermission, Placeholder, UserSettings
Expand Down Expand Up @@ -177,6 +177,18 @@ def test_redirect_preserving_query_parameters(self):
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], redirect + params)

@override_settings(CMS_REDIRECT_TO_LOWERCASE_SLUG=True)
def test_redirecting_to_lowercase_slug(self):
redirect = '/en/one/'
one = create_page("one", "nav_playground.html", "en", published=True,
redirect=redirect)
url = reverse('pages-details-by-slug', kwargs={"slug": "One"})
request = self.get_request(url)
response = details(request, one.get_path())
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], redirect)


def test_login_required(self):
self.create_homepage("page", "nav_playground.html", "en", published=True, login_required=True)
plain_url = '/accounts/'
Expand Down
1 change: 1 addition & 0 deletions cms/utils/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def wrapper():
'COLOR_SCHEME': 'light',
'COLOR_SCHEME_TOGGLE': False,
'REDIRECT_PRESERVE_QUERY_PARAMS': False,
'REDIRECT_TO_LOWERCASE_SLUG': False,
}


Expand Down
13 changes: 13 additions & 0 deletions cms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ def details(request, slug):
# and there's no pages
return _render_welcome_page(request)


if not page and get_cms_setting("REDIRECT_TO_LOWERCASE_SLUG"):
# Redirect to the lowercase version of the slug
if slug.lower() != slug:
# Only redirect if the slug changes
redirect_url = reverse("pages-details-by-slug", kwargs={"slug": slug.lower()})
if get_cms_setting('REDIRECT_PRESERVE_QUERY_PARAMS'):
query_string = request.META.get('QUERY_STRING')
if query_string:
redirect_url += "?" + query_string
return HttpResponseRedirect(redirect_url)


if not page:
# raise 404
_handle_no_page(request)
Expand Down
12 changes: 12 additions & 0 deletions docs/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1200,3 +1200,15 @@ default
``False``

This indicates to the CMS that redirects should preserve the query parameters.


.. setting:: CMS_REDIRECT_TO_LOWERCASE_SLUG

CMS_REDIRECT_TO_LOWERCASE_SLUG
==============================

default
``False``

This indicates to the CMS that it should redirect requests with an non-lowercase
slug to its lowercase version if no page with that slug is found.

0 comments on commit 7e852ec

Please sign in to comment.