Make Django URLs work with or without /

I have a django application that has / at the end of each conf URL. Example:

# user home page
(r'^home/$', 'user_home_page'),

However, I notice that this causes a ton of redirects on my server, because when people do not add /, it redirects them. Is there a way to get it to accept both without redirecting other than execution:

# user home page
(r'^home$', 'user_home_page'),
(r'^home/$', 'user_home_page'),

or should i avoid this kind of url?

+5
source share
6 answers

For now, you can accept both without redirection using:

(r'^home/?$', 'user_home_page'),

This is not a good SEO practice, because it will look like you have duplicate content and your hits will be split between two pages.

+7
source

Django APPEND_SLASH, .

, , , - URL/ (.. ). URL- trailing slash, URL- . URL-, URL- ​​ URL . ( APPEND_SLASH Django, D.)

http://djangobook.com/en/2.0/chapter03/

+5

Python. :

(r'^home(/?)$', 'user_home_page'),
+2

URL-?

. Django .

: https://docs.djangoproject.com/en/1.3/ref/middleware/#module-django.middleware.common

APPEND_SLASH - True, URL- , URLconf, URL- . URL- URLconf, Django URL-. URL .

+2

, /, , , 301 .

There should be only one version of the page, and in most cases the correct solution will be sent to the 301 canonical version.

In some situations with additional tracking parasites, you also need to define the canonical in the head.

Using the consistent URLs in your APP and on the Internet in links pointing to your application, then you will not have many redirects.

0
source

All Articles