简体   繁体   中英

Django alias redirect on wrong url to the correct url

The question may have been asked many times, but I couldn't find something about this which is a basic stuff to do. I have a problem with my website one of the sub urls of google search redirects to the wrong page.

Here are the urls :

www.example.com/login/user/ #wrong url

www.example.com/accounts/login/ #correct url

urls.py : url(r'^accounts/login/$', auth_views.login, name='login'),

I'd like the user to be redirected to the correct url if he access the wrong one. I know that it is possible to do that with views using redirect(reverse('...')) but doing it just for this purpose doesn't sound like the best way.

Is there a way to redirect an user when he enters a wrong url to another one only by using urls.py ?

You would normally do this on your webserver, eg Apache

RewriteRule /login/user/ /accounts/login/ [R=301,L]

That way you don't need to load Django to do a static redirect (you might build up a longer list of these as site-designs evolve).

One solution is to use the RedirectView inside urls.py as shown here in the docs

So, for example:

url(r'^login/user$', RedirectView.as_view(pattern_name='login')),

To urls.py , you can add:

url(r'^login/user/$', auth_views.login, name='login')

It seems like you are aware of that. Did I not understand your question?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM