From 35fa7715a8ddaada4ae9eed6d4fa0fe36efa116b Mon Sep 17 00:00:00 2001 From: Konstantin Date: Sat, 15 Jun 2024 17:25:34 +0200 Subject: [PATCH] catch trailing slash when redirecting --- Makefile | 2 +- blog/middleware.py | 11 +++++++++++ docker-compose.yaml | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index c5cf332..235abc8 100644 --- a/Makefile +++ b/Makefile @@ -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}' diff --git a/blog/middleware.py b/blog/middleware.py index a429360..0f3ec20 100644 --- a/blog/middleware.py +++ b/blog/middleware.py @@ -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 diff --git a/docker-compose.yaml b/docker-compose.yaml index ea891ef..ce07eb2 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -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: