Serve custom 404 page only on subdomain and cname requests
@@ -131,6 +131,24 @@ def server_error_404(request, exception=None, template_name='404.html'): # pyli
else:
return response
+ # Try to serve custom 404 pages if it's a subdomain/cname
+ if request.subdomain or request.cname:
+ return server_error_404_subdomain(request, template_name)
+
+ # Return the default 404 page generated by Read the Docs
+ r = render(request, template_name)
+ r.status_code = 404
+ return r
+
+
+def server_error_404_subdomain(request, template_name='404.html'):
+ """
+ Handler for 404 pages on subdomains.
+
+ Check if the project associated has a custom ``404.html`` on it's project's
+ root (default version) and serve it. Otherwise, fallback to the default
+ ``template_name`` rendered by Django.
+ """
project, path = project_and_path_from_request(request, request.get_full_path())
version_slug = project.get_default_version()
version = project.versions.get(slug=version_slug)
