Index: trac/web/api.py
===================================================================
--- trac/web/api.py	(revision 2729)
+++ trac/web/api.py	(working copy)
@@ -254,6 +254,10 @@
     If the `path` parameter is specified, the path is appended to the URL.
     Otherwise, only a URL with the components scheme, host and port is returned.
     """
+    if hasattr(req, 'base_url'):
+        scheme, host, _, _, _, _ = urlparse.urlparse(req.base_url)
+        return urlparse.urlunparse((scheme, host, path, None, None, None))
+
     host = req.get_header('Host')
     if req.get_header('X-Forwarded-Host'):
         host = req.get_header('X-Forwarded-Host')

