Skip to content

Commit 6664f35

Browse files
ericholscheragjohnson
authored andcommitted
Fix exact redirects. (#3965)
* Fix exact redirects. Exact redirects depend on the full path being set against, so that you can do things like redirect versions. This was broken in a previous refactor. * Use `self.get_full_path` instead of recreating the path * Avoid undefined `full_path`
1 parent 0efc5e8 commit 6664f35

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

readthedocs/redirects/models.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,12 @@ def redirect_page(self, path, language=None, version_slug=None):
143143
version_slug=version_slug)
144144
return to
145145

146-
def redirect_exact(self, path, **__):
147-
if path == self.from_url:
146+
def redirect_exact(self, path, language=None, version_slug=None):
147+
full_path = path
148+
if language and version_slug:
149+
# reconstruct the full path for an exact redirect
150+
full_path = self.get_full_path(path, language, version_slug)
151+
if full_path == self.from_url:
148152
log.debug('Redirecting %s', self)
149153
return self.to_url
150154
# Handle full sub-level redirects

readthedocs/rtd_tests/tests/test_redirects.py

+11
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ def test_redirect_page(self):
147147
self.assertEqual(
148148
r['Location'], 'http://pip.readthedocs.org/en/latest/tutorial/install.html')
149149

150+
@override_settings(USE_SUBDOMAIN=True)
151+
def test_redirect_exact(self):
152+
Redirect.objects.create(
153+
project=self.pip, redirect_type='exact',
154+
from_url='/en/latest/install.html', to_url='/en/latest/tutorial/install.html'
155+
)
156+
r = self.client.get('/en/latest/install.html', HTTP_HOST='pip.readthedocs.org')
157+
self.assertEqual(r.status_code, 302)
158+
self.assertEqual(
159+
r['Location'], 'http://pip.readthedocs.org/en/latest/tutorial/install.html')
160+
150161
@override_settings(USE_SUBDOMAIN=True)
151162
def test_redirect_keeps_version_number(self):
152163
Redirect.objects.create(

0 commit comments

Comments
 (0)