Edgewall Software

Opened 16 years ago

Closed 16 years ago

Last modified 11 years ago

#7500 closed defect (wontfix)

Trac href links point to /trac/trac.cgi/foo under mod_rewrite — at Version 2

Reported by: philpem@… Owned by:
Priority: normal Milestone:
Component: general Version: 0.11
Severity: normal Keywords: mod_rewrite redirect_url script_url
Cc: Branch:
Release Notes:
API Changes:
Internal Changes:

Description (last modified by Remy Blank)

This seems to be somewhat related to bug #2299

I've set up Trac 0.11 in CGI mode on a user account on my dedicated server, using a few rewrite rules to make the URLs a little tidier. To speed things up, the static elements are accessed directly instead of accessing them through the CGI.

Now while I can access e.g. http://dev.example.com/trac/myproject/ fine, all the links on the page go via trac.cgi, so the Roadmap ends up linking to http://dev.example.com/trac/myproject/trac.cgi/roadmap instead of http://dev.example.com/trac/myproject/roadmap.

My rewrite rules are as follows:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /trac/trac.cgi/$1 [L,QSA]
RewriteRule ^$ /trac/trac.cgi [L]

And the pertinent lines from trac.ini:

[trac]
htdocs_location = /trac/static

I've traced the problem down to Trac expecting Apache to set the SCRIPT_URL environment variable, while Apache is actually setting REDIRECT_URL instead.

I've found two solutions to this: 1) Modify trac.cgi to work around the bug This is easy — add these lines just after the comments but before the big try-catch block:

import os
if 'SCRIPT_URL' not in os.environ and 'REDIRECT_URL' in os.environ:
    os.environ['SCRIPT_URL'] = os.environ['REDIRECT_URL']

2) The proper fix. Edit /lib/python*/site-packages/Trac-0.11-py*.egg/trac/web/main.py. Around line 315 you'll see:

script_url = environ.get('SCRIPT_URL')
if script_url is not None:

Change this to say:

script_url = environ.get('SCRIPT_URL')
if script_url is None:
    script_url = environ.get('REDIRECT_URL')
if script_url is not None:

Note the two lines I added. This makes Trac check for SCRIPT_URL first — if it isn't assigned, then it checks for REDIRECT_URL. This should make it possible for Trac to find out about the mod_rewrite rule and amend the URLs it outputs accordingly.

Change History (2)

comment:1 by philpem@…, 16 years ago

OK, it seems Trac didn't like my formatting… Try again…

1) Modify trac.cgi to work around the bug This is easy — add these lines just after the comments but before the big try-catch block:

import os
if 'SCRIPT_URL' not in os.environ and 'REDIRECT_URL' in os.environ:
    os.environ['SCRIPT_URL'] = os.environ['REDIRECT_URL']

2) The proper fix. Edit /lib/python*/site-packages/Trac-0.11-py*.egg/trac/web/main.py. Around line 315 you'll see:

script_url = environ.get('SCRIPT_URL')
if script_url is not None:

Change this to say:

script_url = environ.get('SCRIPT_URL')
if script_url is None:
    script_url = environ.get('REDIRECT_URL')
if script_url is not None:

Note the two lines I added.

comment:2 by Remy Blank, 16 years ago

Description: modified (diff)
Resolution: wontfix
Status: newclosed

CGI is being phased out, as it is very inefficient. So yes, I would recommend to patch your local trac.cgi.

Note: See TracTickets for help on using tickets.