fix redirects from old blog

This commit is contained in:
Konstantin 2024-06-15 17:40:25 +02:00
parent 35fa7715a8
commit 345784f4d5

View file

@ -16,6 +16,7 @@ class BlogRedirectMiddleware:
if (response.status_code == 404 and
not (request.path.startswith('/blog/') or request.path.startswith('/media/'))):
new_path = f'/blog{request.path}'
new_path = new_path.replace('/posts/', '/')
try:
# Check if the new URL with /blog/ exists
resolve(new_path)
@ -24,16 +25,5 @@ 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