Dave Perrett

Rewrite Trac Links to Redmine With Nginx

linux, programming, version control, web

I recently migrated from Trac (at http://recurser.com/trac/ ) to Redmine (at http://recurser.com/code/ ), and wanted the Trac links to redirect to the right place if possible. The nginx rules that follow achieve most of that. As always, there are a few catches:

  • I’m not sure how well the wiki re-writing works, as i don’t really use the wiki much for personal stuff. Trac & Redmine might use a different format for naming pages. There’s only a few wiki pages that get any traffic, so i redirected them explicitly.
  • Redmine doesn’t seem to support repositories with an underscore (‘_’) in the name - i had to rename my repositories to use hyphens instead, & then add specific rewrite rules for those repositories.
  • I haven’t had time to figure out how to rewrite comparisons of selected revisions yet (eg. if we’re now at revision 500, look at diffs for revision 200 vs revision 300). It should handle most simple cases though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
location /trac {
  # Rename 'transmission' to 'clutch'
  rewrite ^/trac/transmission/(.*)$ /trac/clutch/$1;
  # Redmine doesn't support underscores in repository names - i renamed them with hyphens
  rewrite ^/trac/open_gallery/(.*)$ /trac/open_gallery/$1;
  rewrite ^/trac/project_ueler/(.*)$ /trac/project-ueler/$1;
  rewrite ^/trac/scrabble_bot/(.*)$ /trac/scrabble-bot/$1;

  # Clutch-specific wiki stuff gets a reasonable amount of traffic
  rewrite ^/trac/clutch/wiki/FAQ(.*)$ /code/wiki/clutch/FAQ permanent;
  rewrite ^/trac/clutch/wiki/GettingStarted(.*)$ /code/wiki/clutch/Getting_Started permanent;
  rewrite ^/trac/clutch/wiki/About(.*)$ /code/wiki/clutch/About permanent;
  rewrite ^/trac/clutch/wiki/Development(.*)$ /code/wiki/clutch/Development permanent;

  # Specific revision of a file
  rewrite ^/trac/([a-zA-Z0-9\-_]+)/browser/(.+) /code/repositories/entry/$1/$2 permanent;
  # Select versions to diff
  rewrite ^/trac/([a-zA-Z0-9\-_]+)/log/(.+) /code/repositories/changes/$1/$2 permanent;
  # All reports
  rewrite ^/trac/([a-zA-Z0-9\-_]+)/report(.*) /code/projects/$1/issues permanent;
  # Source Browser
  rewrite ^/trac/([a-zA-Z0-9\-_]+)/browser/? /code/repositories/browse/$1 permanent;
  # Revisions
  rewrite ^/trac/([a-zA-Z0-9\-_]+)/timeline(.*) /code/repositories/revisions/$1 permanent;
  #Wiki
  rewrite ^/trac/([a-zA-Z0-9\-_]+)/wiki(.*) /code/wiki/$1 permanent;
  # Roadmap
  rewrite ^/trac/([a-zA-Z0-9\-_]+)/roadmap(.*) /code/projects/gantt/$1 permanent;
  # Tickets - ids won't necessarily match
  rewrite ^/trac/([a-zA-Z0-9\-_]+)/ticket(.*) /code/projects/$1/issues permanent;
  # Login
  rewrite ^/trac/([a-zA-Z0-9\-_]+)/login(.*) /code/login permanent;
  # Changeset for a specific file
  rewrite ^/trac/([a-zA-Z0-9\-_]+)/changeset/([0-9]+)/(.+) /code/repositories/diff/$1/$3?rev=$2 permanent;
  # Changeset
  rewrite ^/trac/([a-zA-Z0-9\-_]+)/changeset/([0-9]+)/? /code/repositories/revision/$1?rev=$2 permanent;
  # Project
  rewrite ^/trac/([a-zA-Z0-9]+)/?(.*) /code/projects/show/$1 permanent;
  # Trac Root
  rewrite ^/trac(.*)$ /code/ permanent;
}

If you want to use these for Apache/mod_rewrite, you can probable change rewrite to RewriteRule , and change permanent to [R=301,L] , eg:

1
RewriteRule ^/trac(.*)$ /code/ [R=301,L]

I haven’t actually tried it with Apache though, so it might require a little tweaking.