Edgewall Software

Ticket #7500 (closed defect: wontfix)

Opened 5 months ago

Last modified 3 months ago

Trac href links point to /trac/trac.cgi/foo under mod_rewrite

Reported by: philpem@… Owned by:
Priority: normal Milestone:
Component: general Version: 0.11
Severity: normal Keywords: mod_rewrite redirect_url script_url
Cc:

Description (last modified by rblank) (diff)

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.

Attachments

Change History

Changed 5 months ago by philpem@…

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.

Changed 3 months ago by rblank

  • status changed from new to closed
  • resolution set to wontfix
  • description modified (diff)

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

Add/Change #7500 (Trac href links point to /trac/trac.cgi/foo under mod_rewrite)

Author



Change Properties
<Author field>
Action
as closed
Next status will be 'reopened'
to The owner will change from (none). Next status will be 'closed'
 
Note: See TracTickets for help on using tickets.