#19277 closed New feature (fixed)
LocaleMiddleware permanent redirects
Reported by: | Owned by: | ||
---|---|---|---|
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 , 12 years ago
Component: | Translations → Internationalization |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → New feature |
Version: | 1.4 → master |
comment:2 by , 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 , 12 years ago
Owner: | changed from | to
---|
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 , 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 , 12 years ago
Here's a pull request with the changes we suggest: https://github.com/django/django/pull/522
comment:6 by , 12 years ago
Has patch: | set |
---|
comment:7 by , 12 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:8 by , 12 years ago
Needs documentation: | set |
---|---|
Triage Stage: | Ready for checkin → Accepted |
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 , 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 , 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 , 12 years ago
I'll leave this one to a more experienced developer.
comment:12 by , 12 years ago
Status: | new → assigned |
---|
comment:13 by , 12 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:14 by , 11 years ago
Owner: | set to |
---|---|
Resolution: | → fixed |
Status: | new → closed |
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.