Opened 10 years ago

Closed 10 years ago

#24967 closed Bug (invalid)

FORCE_SCRIPT_NAME causes HttpResponseRedirect to fail

Reported by: Sean Herron Owned by: nobody
Component: Core (URLs) Version: 1.8
Severity: Normal Keywords: SCRIPT_NAME, FORCE_SCRIPT_NAME, redirect, HttpResponseRedirect
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When FORCE_SCRIPT_NAME is set in settings, redirects using HttpResponseRedirect seem to fail.

Expected behavior:
While FORCE_SCRIPT_NAME is enabled (set to foo), HttpResponseRedirect should return the user to a correct path, prepended with the script name. (eg, /foo/bar).

Actual behavior:
HttpResponseRedirect doubles the script name and redirects the user to /foo/foo/bar.

As a workaround, I created a utility function to reconstruct a full URL to redirect the user to, but this should not be necessary.

from django.shortcuts import redirect
from django.core.urlresolvers import reverse
from django.conf import settings

def absolute_redirect(url, args=None):
    """
    This is a function that I needed to make because
    Django's default redirect function doesn't work properly when
    FORCE_SCRIPT_NAME is set. It returns a full URL for redirection
    """
    
    reverse_url = reverse(url, args=args)

    if settings.URL_ROOT:
        reverse_url = reverse(url, args=args)
        return redirect('%s%s' % (settings.URL_ROOT, reverse_url))
    else:
        return redirect(reverse_url)

Change History (3)

comment:1 by zauddelig, 10 years ago

Can you tell if you have the same problem setting the var to this:

FORCE_SCRIPT_NAME = "/foo/"

If this solved the problem the bug is related to the documentation.

P.S.
FORCE_SCRIPT_NAME replaces some server setted vars like these
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#EnvVar
So it is probably expect to have the same format /foo/var/ else you could have unexpected behaviors like relative-like urls.

Last edited 10 years ago by zauddelig (previous) (diff)

comment:2 by Sean Herron, 10 years ago

The problem appears to happen with any combination of FORCE_SCRIPT_NAME (foo, /foo, or /foo/). I've tested with pretty much any combination of things I could think of, even trying with the full domain name, but haven't been able to get around the issue.

comment:3 by Tim Graham, 10 years ago

Resolution: invalid
Status: newclosed

Please try TicketClosingReasons/UseSupportChannels to ensure you server setup is correct and reopen if you can say more about where the bug lies or where the documentation could be improved. Thanks!

Your issue looks similar to: http://stackoverflow.com/questions/25289880/django-admin-force-script-name-is-appended-twice-to-url-when-posting

Note: See TracTickets for help on using tickets.
Back to Top