Opened 12 years ago

Closed 11 years ago

Last modified 7 years ago

#19277 closed New feature (fixed)

LocaleMiddleware permanent redirects

Reported by: ppetrid@… Owned by: Tim Graham <timograham@…>
Component: Internationalization Version: dev
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

Description

the process_response() method of django.middleware.locale.LocaleMiddleware redirects urls as follows when using i18n_patterns:
http://example.com/whatever/ becomes http://example.com/en/whatever/

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 by Claude Paroz, 12 years ago

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 www.yourdomain.com is kept in the search index instead of www.yourdomain.com/en/.

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 by anonymous, 12 years ago

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 by EmilStenstrom, 12 years ago

Owner: changed from nobody to EmilStenstrom

Hi!

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 by ppetrid@…, 12 years ago

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 by EmilStenstrom, 12 years ago

Here's a pull request with the changes we suggest: https://github.com/django/django/pull/522

comment:6 by EmilStenstrom, 12 years ago

Has patch: set

comment:7 by Jannis Leidel, 12 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Preston Holmes, 12 years ago

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 https://docs.djangoproject.com/en/dev/topics/i18n/translation/#how-django-discovers-language-preference

comment:9 by Aymeric Augustin, 12 years ago

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 by Aymeric Augustin, 12 years ago

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 by EmilStenstrom, 12 years ago

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

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

comment:12 by EmilStenstrom, 12 years ago

Status: newassigned

comment:13 by EmilStenstrom, 12 years ago

Owner: EmilStenstrom removed
Status: assignednew

comment:14 by Tim Graham <timograham@…>, 11 years ago

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

In 7a97df190cb993056aa097befb3813100cfc286e:

Fixed #19277 -- Added LocaleMiddleware.response_redirect_class

Thanks ppetrid at yawd.eu for the suggestion.

comment:15 by William Hingston, 7 years ago

This still needs to be added to the documentation.

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