skip to content
Notes && Anecdotes
A decorative, semi-related stock photo that adds less value than this comment.

django rewrite / redirect to new urls

djangoredirectrewriteurls

I changed the MEDIA_URL and STATIC_URL, but wanted the previously used urls “/media/” and “/static/” to rewrite to the new url. It would better be done via your DNS settings, or with an .htaccess file, but can also be done via **urls.py **if your circumstances don’t allow the other two.

from django.conf import settings
from django.views.generic.base import RedirectView

[...]

if settings.MEDIA_URL != "/media/":
    urlpatterns += url(
        r'^media/(?P<path>.\*),
        RedirectView.as_view(
            url=settings.MEDIA_URL + '%(path)s',
            permanent=True
        ),
        name='cloud-media'
    )

if settings.STATIC_URL != "/static/":
    urlpatterns += url(
        r'^static/(?P<path>.\*),
        RedirectView.as_view(
            url=settings.STATIC_URL + '%(path)s', permanent=True
        ),
        name='cloud-static'
    )