First perform "+ /index.html" files before user redirects
Check if `/index.html` or `/README.html` exist before performing
user-define redirects. This keep the behavior that we have on NGINX
with the `tryfiles` config. If any of these files was found in the
filesystem we just serve it, otherwise we performed the user-defined
redirects.
@@ -164,6 +164,7 @@ class ServeError404Base(ServeRedirectMixin, View):
the Docs default page (Maze Found) is rendered by Django and served.
"""
# pylint: disable=too-many-locals
+ log.info('Executing handler404. proxito_path=%s', proxito_path)
# Parse the URL using the normal urlconf, so we get proper subdomain/translation data
_, __, kwargs = url_resolve(
@@ -192,17 +193,6 @@ class ServeError404Base(ServeRedirectMixin, View):
# fine always adding the ``/`` to the beginning.
filepath = '/' + filepath.lstrip('/')
- # Check and perform redirects on 404 handler
- redirect_path, http_status = self.get_redirect(
- project=final_project,
- lang_slug=lang_slug,
- version_slug=version_slug,
- filepath=filepath,
- full_path=proxito_path,
- )
- if redirect_path and http_status:
- return self.get_redirect_response(request, redirect_path, proxito_path, http_status)
-
storage_root_path = final_project.get_storage_path(
type_='html',
version_slug=version_slug,
@@ -223,7 +213,7 @@ class ServeError404Base(ServeRedirectMixin, View):
)
if storage.exists(storage_filename_path):
log.info(
- 'Redirecting to index file: project=%s version=%s, url=%s',
+ 'Redirecting to index file: project=%s version=%s, storage_path=%s',
final_project.slug,
version_slug,
storage_filename_path,
@@ -233,10 +223,27 @@ class ServeError404Base(ServeRedirectMixin, View):
if tryfile == 'README.html':
new_path = os.path.join(parts.path, tryfile)
else:
- new_path = parts.path + '/'
+ new_path = parts.path.rstrip('/') + '/'
new_parts = parts._replace(path=new_path)
- resp = HttpResponseRedirect(new_parts.geturl())
- return resp
+ redirect_url = new_parts.geturl()
+
+ # TODO: decide if we need to check for infinite redirect here
+ # (from URL == to URL)
+ return HttpResponseRedirect(redirect_url)
+
+ # Check and perform redirects on 404 handler
+ # NOTE: this redirect check must be done after trying files like
+ # ``index.html`` and ``README.html`` to emulate the behavior we had when
+ # serving directly from NGINX without passing through Python.
+ redirect_path, http_status = self.get_redirect(
+ project=final_project,
+ lang_slug=lang_slug,
+ version_slug=version_slug,
+ filepath=filepath,
+ full_path=proxito_path,
+ )
+ if redirect_path and http_status:
+ return self.get_redirect_response(request, redirect_path, proxito_path, http_status)
# If that doesn't work, attempt to serve the 404 of the current version (version_slug)
# Secondly, try to serve the 404 page for the default version
