Opened 5 years ago

Closed 4 years ago

Last modified 2 weeks ago

#19277 closed New feature (fixed)

LocaleMiddleware permanent redirects

Reported by: ppetrid@… Owned by: Tim Graham <timograham@…>
Component: Internationalization Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no


the process_response() method of django.middleware.locale.LocaleMiddleware redirects urls as follows when using i18n_patterns: becomes

These sorts of redirects must be permanent (301) and not temporary (302). The reasoning behind this: 302 redirects suggest that the requested page does have a content of its own (therefore it is a perfectly valid permalink) but for some reason we are temporarily redirected to another page. This is not true in our case, /whatever/ is a duplicate of /en/whatever/ and users will always get redirected as long as the middleware is enabled. Current behavior can be confusing to web crawlers as well.

The HttpResponseRedirect response should become HttpResponsePermanentRedirect to fix this. I'll provide the patch if ticket gets accepted.

Change History (15)

comment:1 Changed 5 years ago by Claude Paroz

Component: TranslationsInternationalization
Triage Stage: UnreviewedAccepted
Type: UncategorizedNew feature
Version: 1.4master

I don't think that a permanent redirect is always wanted. Depending on the user agent, the redirected URL will change. You talk about web crawlers, but I guess you'd better find that is kept in the search index instead of

Google itself is using 302 redirects to redirect to country specific interfaces.

I'm afraid there are no true right answer to this. We could even imagine using the 300 code. I'd be in favour of setting a status_code class attribute for LocaleMiddleware, so it is easy for subclasses to customize it. Accepting on this base.

comment:2 Changed 5 years ago by anonymous

Agreed on the setting. Normaly you would't want both / and /en/ indexed since it will split your backlinks, pagerank etc (and they'll both get indexed unless the developer takes further action). The agent is indeed an issue and when it comes to that 302 redirects are more suitable. As for google, I quit using them as an example the day I found google is the first to break their own guidelines.. :)

comment:3 Changed 5 years ago by EmilStenstrom

Owner: changed from nobody to EmilStenstrom


We're sprinting (Stockholm) at the moment and plan to solve this bug like this:

Instead of setting a status_code attribute, we would like to use a redirect_class. The reason for this is that HttpResponseRedirectBase has built in protection against unsafe protocol redirections. If we use a HttpResponse object and let the user supply their own status code they will probably miss that security issue, which would be a shame.

comment:4 Changed 5 years ago by ppetrid@…

Hello, just my 2 cents: HttpResponseRedirectBase subclasses must be used no matter what. I favor the redirect_class attribute, setting the class through an attribute is a common practice in many aspects over the framework. Besides that, one might use custom HttpResponseRedirects.

comment:5 Changed 5 years ago by EmilStenstrom

Here's a pull request with the changes we suggest:

comment:6 Changed 5 years ago by EmilStenstrom

Has patch: set

comment:7 Changed 5 years ago by Jannis Leidel

Triage Stage: AcceptedReady for checkin

comment:8 Changed 5 years ago by Preston Holmes

Needs documentation: set
Triage Stage: Ready for checkinAccepted

I agree that a redirect_class is good architecture for this - but the only point of adding this is as some form of API - and if the decision is being made to add API - it needs documentation.

Perhaps as a note in

comment:9 Changed 5 years ago by Aymeric Augustin

I think it makes sense to make the implementation more subclassing-friendly even without committing to support a given API. We've done that before.

comment:10 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset
Patch needs improvement: set

It'd be nice to make the implementation, tests (and docs if we decide to document this API) consistent between the locale and redirects middleware (#19321).

The pull request is out of date.

comment:11 Changed 5 years ago by EmilStenstrom

I'll leave this one to a more experienced developer.

Version 0, edited 5 years ago by EmilStenstrom (next)

comment:12 Changed 5 years ago by EmilStenstrom

Status: newassigned

comment:13 Changed 5 years ago by EmilStenstrom

Owner: EmilStenstrom deleted
Status: assignednew

comment:14 Changed 4 years ago by Tim Graham <timograham@…>

Owner: set to Tim Graham <timograham@…>
Resolution: fixed
Status: newclosed

In 7a97df190cb993056aa097befb3813100cfc286e:

Fixed #19277 -- Added LocaleMiddleware.response_redirect_class

Thanks ppetrid at for the suggestion.

comment:15 Changed 2 weeks ago by hingston

This still needs to be added to the documentation.

Note: See TracTickets for help on using tickets.
Back to Top