catch trailing slash when redirecting

This commit is contained in:
Konstantin 2024-06-15 17:25:34 +02:00
parent c93d55dacc
commit 35fa7715a8
3 changed files with 13 additions and 2 deletions

View file

@ -1,5 +1,5 @@
.PHONY: help build publish
VERSION = 1.1.2
VERSION = 1.1.3
help:
@perl -nle'print $& if m{^[a-zA-Z_-]+:.*?## .*$$}' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

View file

@ -24,5 +24,16 @@ class BlogRedirectMiddleware:
except Http404:
# If the new URL doesn't exist, return the original 404 response
pass
# handle old blog urls e.g. `https://www.iamkonstantin.eu/posts/safari-local-overrides/`
# replace /posts/ with /blog/ and remove trailing /
if response.status_code == 404 and request.path.startswith('/posts/'):
new_path = f'/blog{request.path[6:]}'
if new_path.endswith('/'):
new_path = new_path[:-1]
try:
resolve(new_path)
return HttpResponseRedirect(new_path)
except Http404:
pass
return response

View file

@ -2,7 +2,7 @@ version: '3.8'
services:
wagtail:
image: code.headbright.be/konstantin/iamkonstantin:1.0.6
image: code.headbright.be/konstantin/iamkonstantin:1.1.2
container_name: wagtail
restart: always
volumes: