Repository: mdn/kuma

Issue:
Wrong redirect on localized home page
Examples:
```
▶ curl -I https:/developer.mozilla.org/fr
HTTP/2 301
location: /fr/docs/fr
server: gunicorn/20.0.4
```
The exact same is happening to `ja`

But other locales seem fine, e.g.
```
▶ curl -I https:/developer.mozilla.org/de
HTTP/2 301
location: /de/
server: gunicorn/20.0.4
```
The exact same is happening to `sv-SE` and `en-US`. 


Discussion:
Here's the complete list of locales and how they redirect
```
en-US   301 /en-US/         gunicorn/20.0.4
ar      301 /ar/            gunicorn/20.0.4
bg      301 /bg/            gunicorn/20.0.4
bm      301 /bm/            gunicorn/20.0.4
bn      301 /bn/            gunicorn/20.0.4
ca      301 /ca/            gunicorn/20.0.4
de      301 /de/            gunicorn/20.0.4
el      301 /el/            gunicorn/20.0.4
es      301 /es/            gunicorn/20.0.4
fa      301 /fa/            gunicorn/20.0.4
fi      301 /fi/docs/fi     gunicorn/20.0.4
fr      301 /fr/docs/fr     gunicorn/20.0.4
he      301 /he/docs/he     gunicorn/20.0.4
hi-IN   301 /hi-IN/         gunicorn/20.0.4
hu      301 /hu/            gunicorn/20.0.4
id      301 /id/            gunicorn/20.0.4
it      301 /it/            gunicorn/20.0.4
ja      301 /ja/docs/ja     gunicorn/20.0.4
kab     301 /kab/           gunicorn/20.0.4
ko      301 /ko/            gunicorn/20.0.4
ms      301 /ms/            gunicorn/20.0.4
my      301 /my/            gunicorn/20.0.4
nl      301 /nl/            gunicorn/20.0.4
pl      301 /pl/            gunicorn/20.0.4
pt-PT   301 /pt-PT/         gunicorn/20.0.4
pt-BR   301 /pt-BR/         gunicorn/20.0.4
ru      301 /ru/            gunicorn/20.0.4
sv-SE   301 /sv-SE/         gunicorn/20.0.4
th      301 /th/            gunicorn/20.0.4
tr      301 /tr/docs/Tr     gunicorn/20.0.4
uk      301 /uk/            gunicorn/20.0.4
vi      301 /vi/            gunicorn/20.0.4
zh-CN   301 /zh-CN/         gunicorn/20.0.4
zh-TW   301 /zh-TW/docs/zh-tw gunicorn/20.0.4
```

This is if you `GET https://developer.mozilla.org/LOCALE` (no trailing `/`)
Clearly, it's happening in Kuma. But why?! And why is it not happening in kuma on localhost?
If I change the domain to `localhost.org:8000` I get this output:
```
en-US   301 /en-US/ WSGIServer/0.2 CPython/3.8.0
ar      301 /ar/    WSGIServer/0.2 CPython/3.8.0
bg      301 /bg/    WSGIServer/0.2 CPython/3.8.0
bm      301 /bm/    WSGIServer/0.2 CPython/3.8.0
bn      301 /bn/    WSGIServer/0.2 CPython/3.8.0
ca      301 /ca/    WSGIServer/0.2 CPython/3.8.0
de      301 /de/    WSGIServer/0.2 CPython/3.8.0
el      301 /el/    WSGIServer/0.2 CPython/3.8.0
es      301 /es/    WSGIServer/0.2 CPython/3.8.0
fa      301 /fa/    WSGIServer/0.2 CPython/3.8.0
fi      301 /fi/    WSGIServer/0.2 CPython/3.8.0
fr      301 /fr/    WSGIServer/0.2 CPython/3.8.0
he      301 /he/    WSGIServer/0.2 CPython/3.8.0
hi-IN   301 /hi-IN/ WSGIServer/0.2 CPython/3.8.0
hu      301 /hu/    WSGIServer/0.2 CPython/3.8.0
id      301 /id/    WSGIServer/0.2 CPython/3.8.0
it      301 /it/    WSGIServer/0.2 CPython/3.8.0
ja      301 /ja/    WSGIServer/0.2 CPython/3.8.0
kab     301 /kab/   WSGIServer/0.2 CPython/3.8.0
ko      301 /ko/    WSGIServer/0.2 CPython/3.8.0
ms      301 /ms/    WSGIServer/0.2 CPython/3.8.0
my      301 /my/    WSGIServer/0.2 CPython/3.8.0
nl      301 /nl/    WSGIServer/0.2 CPython/3.8.0
pl      301 /pl/    WSGIServer/0.2 CPython/3.8.0
pt-PT   301 /pt-PT/ WSGIServer/0.2 CPython/3.8.0
pt-BR   301 /pt-BR/ WSGIServer/0.2 CPython/3.8.0
ru      301 /ru/    WSGIServer/0.2 CPython/3.8.0
sv-SE   301 /sv-SE/ WSGIServer/0.2 CPython/3.8.0
th      301 /th/    WSGIServer/0.2 CPython/3.8.0
tr      301 /tr/    WSGIServer/0.2 CPython/3.8.0
uk      301 /uk/    WSGIServer/0.2 CPython/3.8.0
vi      301 /vi/    WSGIServer/0.2 CPython/3.8.0
zh-CN   301 /zh-CN/ WSGIServer/0.2 CPython/3.8.0
zh-TW   301 /zh-TW/ WSGIServer/0.2 CPython/3.8.0
```
Ah ha!
```
mysql> select locale, slug from wiki_document where slug in ('en-US', 'ar', 'bg', 'bm', 'bn', 'ca', 'de', 'el', 'es', 'fa', 'fi', 'fr', 'he', 'hi-IN', 'hu', 'id', 'it', 'ja', 'kab', 'ko', 'ms', 'my', 'nl', 'pl', 'pt-PT', 'pt-BR', 'ru', 'sv-SE', 'th', 'tr', 'uk', 'vi', 'zh-CN', 'zh-TW');
+--------+-------+
| locale | slug  |
+--------+-------+
| fi     | fi    |
| fr     | fr    |
| he     | he    |
| en-US  | id    |
| ja     | ja    |
| tr     | Tr    |
| zh-TW  | zh-tw |
+--------+-------+
7 rows in set (0.01 sec)
```

The lines of code that's causing it in Kuma are these:
https://github.com/mdn/kuma/blob/3d52c833d35e28b079f3ec252d986915d275ecc0/kuma/wiki/views/legacy.py#L80-L88

Here's what one of them look like. They're not all redirects:
```
mysql> select html from wiki_document where locale='fr' and slug='fr';
+--------------------------------------------------------+
| html                                                   |
+--------------------------------------------------------+
| <p>REDIRECT <a class="redirect" href="/fr/">fr</a></p> |
+--------------------------------------------------------+
1 row in set (0.00 sec)
```

But the `fi` (Finnish?) page is not a redirect but its HTML looks weirdly ancient. 
```html
<div class="mainpage">
 <table class="features-table">
  <tbody>
   <tr>
    <td>
     <p><img alt="Image:Firefoxlogo2.png" class="internal" src="/@api/deki/files/1155/=Firefoxlogo2.png"></p>
     <h4 id="Firefox_2_kehitt.C3.A4jille" name="Firefox_2_kehitt.C3.A4jille"><a href="/fi/Firefox_2_kehittäjille" title="fi/Firefox_2_kehittäjille">Firefox 2 kehittäjille</a></h4>
     <p>Firefox 2 on julkaistu, ja on <strong><a class="external" href="http://www.mozilla.com/en-US/firefox/all.html">ladattavissa</a></strong>. Katso <a href="/fi/Firefox_2_kehittäjille" title="fi/Firefox_2_kehittäjille">Firefox 2 kehittäjille</a> (osin suomennettu) saadaksesi lisätietoa</p>
    </td>
   </tr>
  </tbody>
 </table>
 <p><span class="comment">Categories</span> <span class="comment">Interwiki Language Links</span></p>
</div>
<p> </p>
```
Note the `http://www.mozilla.com...` links. 
I boldly just delete that `fi` document:
```
>>> doc = Document.objects.get(locale='fi', slug='fi')
>>> doc.is_redirect
False
>>> doc.html
'<div class="mainpage">\n <table class="features-table">\n  <tbody>\n   <tr>\n    <td>\n     <p><img alt="Image:Firefoxlogo2.png" class="internal" src="/@api/deki/files/1155/=Firefoxlogo2.png"></p>\n     <h4 id="Firefox_2_kehitt.C3.A4jille" name="Firefox_2_kehitt.C3.A4jille"><a href="/fi/Firefox_2_kehittäjille" title="fi/Firefox_2_kehittäjille">Firefox 2 kehittäjille</a></h4>\n     <p>Firefox 2 on julkaistu, ja on <strong><a class="external" href="http://www.mozilla.com/en-US/firefox/all.html">ladattavissa</a></strong>. Katso <a href="/fi/Firefox_2_kehittäjille" title="fi/Firefox_2_kehittäjille">Firefox 2 kehittäjille</a> (osin suomennettu) saadaksesi lisätietoa</p>\n    </td>\n   </tr>\n  </tbody>\n </table>\n <p><span class="comment">Categories</span> <span class="comment">Interwiki Language Links</span></p>\n</div>\n<p>\xa0</p>'
>>> doc.delete()
```

Now it redirects sensibly:
```
▶ curl -I https://developer.mozilla.org/fi
HTTP/2 301
location: /fi/
server: gunicorn/20.0.4
...
```
And, I've now actually deleted the `ja` and the `fr` documents too. 

Resolving patch:
diff --git a/kuma/wiki/views/legacy.py b/kuma/wiki/views/legacy.py
--- a/kuma/wiki/views/legacy.py
+++ b/kuma/wiki/views/legacy.py
@@ -5,7 +5,7 @@
 from kuma.core.decorators import shared_cache_control
 
 from ..constants import LEGACY_MINDTOUCH_NAMESPACES
-from ..models import Document, Revision
+from ..models import Revision
 
 
 # Legacy MindTouch redirects.
@@ -77,15 +77,7 @@ def mindtouch_to_kuma_url(locale, path):
         if namespace in LEGACY_MINDTOUCH_NAMESPACES:
             return mindtouch_namespace_to_kuma_url(locale, namespace, slug)
 
-    # Last attempt: we try the request locale as the document locale,
-    # and see if that matches something.
-    try:
-        doc = Document.objects.get(slug=path, locale=locale)
-    except Document.DoesNotExist:
-        return None
-
-    location = doc.get_absolute_url()
-    return location
+    return None
 
 
 @shared_cache_control(s_maxage=60 * 60 * 24 * 30)


Test patch:
diff --git a/kuma/wiki/tests/test_views.py b/kuma/wiki/tests/test_views.py
--- a/kuma/wiki/tests/test_views.py
+++ b/kuma/wiki/tests/test_views.py
@@ -2807,31 +2807,6 @@ def test_namespace_urls(self):
             assert 301 == resp.status_code
             assert resp["Location"] == namespace_test["kuma"]
 
-    def test_document_urls(self):
-        """Check the url redirect to proper document when the url like
-        /<locale>/<document_slug>"""
-        d = document(locale="zh-CN")
-        d.save()
-        mt_url = "/{locale}/{slug}".format(locale=d.locale, slug=d.slug)
-        resp = self.client.get(mt_url, follow=True, HTTP_HOST=settings.WIKI_HOST)
-        assert resp.status_code == 200
-
-        # Check the last redirect chain url is correct document url
-        last_url = resp.redirect_chain[-1][0]
-        assert last_url == d.get_absolute_url()
-
-    def test_view_param(self):
-        d = document()
-        d.locale = settings.WIKI_DEFAULT_LANGUAGE
-        d.slug = "HTML/HTML5"
-        d.title = "HTML 5"
-        d.save()
-        mt_url = "/en-US/%s?view=edit" % (d.slug,)
-        resp = self.client.get(mt_url, HTTP_HOST=settings.WIKI_HOST)
-        assert 301 == resp.status_code
-        expected_url = d.get_absolute_url("wiki.edit")
-        assert resp["Location"] == expected_url
-
 
 @override_config(KUMASCRIPT_TIMEOUT=5.0, KUMASCRIPT_MAX_AGE=600)
 class DeferredRenderingViewTests(UserTestCase, WikiTestCase):