Edgewall Software

Opened 17 years ago

Last modified 4 years ago

#2299 new defect

Ability to override SCRIPT_NAME from base_url

Reported by: oetiker@… Owned by:
Priority: normal Milestone: unscheduled
Component: general Version: 0.11.6
Severity: normal Keywords: review fcgi patch
Cc: CarstenFuchs@…, ethan.jucovy@… Branch:
Release Notes:
API Changes:
Internal Changes:


I am using mod_rewrite to hide the fcgi trac script … this works fine, except that trac uses absolte urls in its generated content … so the name of the fcgi script will be exposed all the same … with this patch I can set the TRAC_BASE_URL environment variable to tell trac what base URL it should be using.

--- temp/trac/trac-0.9/trac/web/cgi_frontend.py 2005-10-31 19:37:51.000000000 +0100
+++ pack/trac-0.9/lib/python2.3/site-packages/trac/web/cgi_frontend.py  2005-11-02 23:30:52.393286000 +0100
@@ -49,6 +49,10 @@
         self.args = self._getFieldStorage()
         self.cgi_location = self.__environ.get('SCRIPT_NAME')
+        if 'TRAC_BASE_URL' in os.environ:
+            self.cgi_location = os.environ['TRAC_BASE_URL'];
         self.idx_location = self.cgi_location
         self.path_info = self.__environ.get('PATH_INFO', '')

Attachments (0)

Change History (23)

comment:1 by Matthew Good, 17 years ago

Resolution: worksforme
Status: newclosed

Instead of mod_rewrite, use a ScriptAlias command to point to the fcgi script and the CGI SCRIPT_NAME variable will be set appropriately.

To run Trac at the root URL:

ScriptAlias / /usr/share/trac/cgi-bin/trac.fcgi

comment:2 by jo@…, 17 years ago

Resolution: worksforme
Status: closedreopened

But if one uses RewriteEngine to hide user names in suexec configurations, ScriptAlias does not work. It breaks the suexec mechansim. In such a situation BASE_URL would be a good idea.

comment:3 by sid, 16 years ago

Keywords: review added

Patch attached, so should be reviewed.

comment:4 by Christopher Lenz, 16 years ago

Resolution: wontfix
Status: reopenedclosed
  1. the patch is outdated (would need to be updated for 0.10)
  2. this functionality is available by use of the base_url option in trac.ini

comment:5 by tobi@…, 16 years ago

Resolution: wontfix
Severity: minornormal
Status: closedreopened
Version: 0.90.10

base_url in trac.ini does not allow to override what SCRIPT_NAME returns … which is necessary when working without ScriptAlias (see above). The following patch adds the necessary functionallity for trac 0.10:

--- trac/web/api.py.orig        2006-09-21 20:32:16.000000000 +0200
+++ trac/web/api.py     2006-10-22 14:00:07.000000000 +0200
@@ -223,7 +223,15 @@
                                'has not logged in using HTTP authentication')
     scheme = property(fget=lambda self: self.environ['wsgi.url_scheme'],
                       doc='The scheme of the request URL')
-    base_path = property(fget=lambda self: self.environ.get('SCRIPT_NAME', ''),
+    def _get_base_url(self):
+       from urlparse import urlparse
+       if self.base_url:
+               return urlparse(self.base_url.encode('latin-1'))[2]
+       else:
+               return self.environ.get('SCRIPT_NAME')
+    base_path = property(fget=_get_base_url,
                          doc='The root path of the application')
     server_name = property(fget=lambda self: self.environ['SERVER_NAME'],
                            doc='Name of the server')

comment:6 by sid, 16 years ago

Keywords: fcgi added

comment:7 by dom@…, 16 years ago

I would like this patch to make it in - it is as far as I can see the only way to make nice URLs with fcgi and mod_rewrite. I am currently manually patching to achieve this aim.

comment:8 by Christian Boos, 16 years ago

Milestone: 0.10.4

Could someone familiar with fcgi review this?

comment:9 by Tim Hatch, 16 years ago

Summary: patch enhancing trac for nice urlsAbility to override SCRIPT_NAME from base_url

A slightly related ticket is #2418, which strangely shows up in searches while this one initially didn't.

I run Trac under mod_python and thus can't have this issue (modpython_frontend bails if TRAC_URI_ROOT and base_url don't match), however if this issue still needs fixing I came up with a patch with tests that doesn't reimport urlparse.

in reply to:  8 comment:10 by Matthew Good, 16 years ago

Owner: changed from Jonas Borgström to Matthew Good
Status: reopenednew

Replying to cboos:

Could someone familiar with fcgi review this?

Yeah, I'll do it.

comment:11 by Matthew Good, 15 years ago

Resolution: fixed
Status: newclosed

This has been fixed with r4718:4719 and r4720.

This uses Apache's SCRIPT_URL variable which is specifically intended for reconstructing the logical URL when rewriting has been used, so I think this a cleaner solution.

comment:12 by James Teh <jamie@…>, 14 years ago

Component: generaladmin/console
Milestone: 0.10.4
Priority: normalhighest
Severity: normalblocker
Type: enhancementdefect
Version: 0.10

If you are using Apache mod_rewrite and the script path is still exposed, be aware that the SCRIPT_URL variable is not set by mod_rewrite if the RewriteEngine on directive is used in .htaccess or directory context. SCRIPT_URL is only set if RewriteEngine on is used in the server or VirtualHost context. This is an extremely frustrating issue in mod_rewrite. I thought this worth noting here to hopefully save others some agony in the future. :)

comment:13 by James Teh <jamie@…>, 14 years ago

Component: admin/consolegeneral
Milestone: 0.10.5
Priority: highestnormal
Severity: blockernormal
Type: defectenhancement
Version: 0.10

Err… it'd be nice if I didn't change all the properties by mistake. I think I just found a Firefox bug. :) Sorry people. I can't change the milestone back to 0.10.4, which is what it should be.

comment:14 by Christian Boos, 13 years ago


(fixing milestone)

comment:15 by Carsten Fuchs <CarstenFuchs@…>, 12 years ago

Cc: CarstenFuchs@… added
Resolution: fixed
Status: closedreopened
Type: enhancementdefect

I have been experiencing the same situation as the ticket reporter, see my mails to the trac-users mailing list.

After long and daunting research I found that James Teh is right in his previous comment and that this ticket addresses the root of the problem and thus should be reopened:

When you use mod_rewrite for pretty Trac URLs as described at TracPrettyUrls, and SCRIPT_URL is not set as described by James Teh, then SCRIPT_NAME is always the rewritten name, which in turn causes "unprettied" or "absolute" URLs in the generated content page.

Work-around: Insert line

os.environ['SCRIPT_NAME'] = ""

into file trac.fcgi.

I'm unfortunately not safe enough in Python in order to know better or even to provide a patch, but wouldn't using REQUEST_URI instead of SCRIPT_NAME help here? Alternatively, please apply the above original patch.

in reply to:  15 comment:16 by Carsten Fuchs <CarstenFuchs@…>, 12 years ago

Work-around: Insert line os.environSCRIPT_NAME = "" into file trac.fcgi.

The above work-around unfortunately works only for trac.cgi, not for trac.fcgi, sorry.

in reply to:  15 comment:17 by Martin.vGagern@…, 12 years ago

Replying to Carsten Fuchs <CarstenFuchs@…>:

wouldn't using REQUEST_URI instead of SCRIPT_NAME help here?

One problem is that REQUEST_URI is URL-encoded while SCRIPT_NAME and PATH_INFO are not. So in order to find the base of the Trac environment, one would have to strip the query from REQUEST_URI, URL-decode the rest, strip off PATH_INFO, and probably URL-encode the remainder again. So it's not easy, as I learned the hard way in comment:4:ticket:8328.

comment:18 by Remy Blank, 12 years ago

Milestone: 0.12next-major-0.1X

We currently don't have a complete solution, and we're a bit short on the testing side for frontends other than mod_python or mod_wsgi. Postponing…

comment:19 by Christian Boos, 12 years ago

Milestone: next-major-0.1Xunscheduled

in reply to:  18 comment:20 by Mike Kelly <pioto@…>, 11 years ago

Replying to rblank:

We currently don't have a complete solution, and we're a bit short on the testing side for frontends other than mod_python or mod_wsgi. Postponing…

If you need volunteers to test patches on mod_fcgi, I can help with that. I'm not familiar enough with the trac codebase to develop a patch myself, but if you gave me pointers I could try to hack something up.

I think a solution to this would be helpful for use in shared hosting environments, where using ScriptAlias or adding anything to the apache config isn't an option, but .htaccess is.

comment:21 by ethan.jucovy@…, 10 years ago

Cc: ethan.jucovy@… added

comment:22 by Ryan J Ollos, 7 years ago

Owner: Matthew Good removed
Status: reopenednew

comment:23 by figaro, 6 years ago

Keywords: patch added

Modify Ticket

Change Properties
Set your email in Preferences
as new The ticket will remain with no owner.
The ticket will be disowned.
as The resolution will be set. Next status will be 'closed'.
The owner will be changed from (none) to anonymous. Next status will be 'assigned'.

Add Comment

E-mail address and name can be saved in the Preferences .
Note: See TracTickets for help on using tickets.