fix redirects from old blog
This commit is contained in:
parent
35fa7715a8
commit
345784f4d5
1 changed files with 1 additions and 11 deletions
|
@ -16,6 +16,7 @@ class BlogRedirectMiddleware:
|
||||||
if (response.status_code == 404 and
|
if (response.status_code == 404 and
|
||||||
not (request.path.startswith('/blog/') or request.path.startswith('/media/'))):
|
not (request.path.startswith('/blog/') or request.path.startswith('/media/'))):
|
||||||
new_path = f'/blog{request.path}'
|
new_path = f'/blog{request.path}'
|
||||||
|
new_path = new_path.replace('/posts/', '/')
|
||||||
try:
|
try:
|
||||||
# Check if the new URL with /blog/ exists
|
# Check if the new URL with /blog/ exists
|
||||||
resolve(new_path)
|
resolve(new_path)
|
||||||
|
@ -24,16 +25,5 @@ class BlogRedirectMiddleware:
|
||||||
except Http404:
|
except Http404:
|
||||||
# If the new URL doesn't exist, return the original 404 response
|
# If the new URL doesn't exist, return the original 404 response
|
||||||
pass
|
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
|
return response
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue