Index: /branches/locker-dev/locker/deploy/trac/trac.fcgi
===================================================================
--- /branches/locker-dev/locker/deploy/trac/trac.fcgi	(revision 1527)
+++ /branches/locker-dev/locker/deploy/trac/trac.fcgi	(revision 1528)
@@ -3,4 +3,5 @@
 import os, os.path, sys
 from trac.web import fcgi_frontend
+import urlparse
 
 env_path = os.getcwd()+'/tracdata'
@@ -47,4 +48,21 @@
         and environ['REDIRECT_URL'].endswith(environ['PATH_INFO'])):
         environ['SCRIPT_NAME'] = environ['REDIRECT_URL'][:-len(environ['PATH_INFO'])]
+
+    # If the referrer has our hostname and path, rewrite it to have
+    # the right protocol and port, too.  This lets the login link go
+    # to the right page.
+    if 'HTTP_REFERER' in environ:
+        referrer = urlparse.urlsplit(environ['HTTP_REFERER'])
+        base = urlparse.urlsplit(
+            ('https://' if environ.get('HTTPS') == 'on' else 'http://') +
+            environ['HTTP_HOST'] +
+            environ['SCRIPT_NAME'])
+        if referrer.hostname == base.hostname and \
+           (referrer.path == base.path or
+            referrer.path.startswith(base.path + '/')):
+            environ['HTTP_REFERER'] = urlparse.urlunsplit(
+                (base.scheme, base.netloc,
+                 referrer.path, referrer.query, referrer.fragment))
+
     return fcgi_frontend.dispatch_request(environ, start_response)
 
