Edgewall Software

Prettier URLs for Trac

There are several ways to obtain clean URLs in Apache:



Using mod_rewrite through httpd.conf

If you have access to your Apache config, load the module mod_rewrite, then place this in the config:

RewriteEngine On
<Location "/cgi-bin/trac.cgi">
  SetEnv TRAC_ENV "/path/to/trac/env"

ServerName <hostname>
Alias /trac "/usr/share/trac/htdocs"

ScriptAlias /cgi-bin /path/to/cgi-bin
RewriteRule ^/$ /cgi-bin/trac.cgi [R]

If you have a subdomain, it should go inside VirtualHost tags, see Apache documentation.

Or using ScriptAliasMatch you can redirect all request coming to / without rewriting the URL to the user:

SetEnv TRAC_ENV "/var/to/trac"

ScriptAliasMatch ^/(.*) /path/to/trac.cgi/$1

Using mod_rewrite from .htaccess files

Make an .htaccess file or add the following lines into existing if it's already present in your site directory with trac.fcgi:

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

The RewriteCond strings tell Apache to not fire redirection to Trac script if there is an existing file or directory with the requested name. The last rule works always and redirects root site requests to the TracCGI script.

If you want to explicitly exclude some paths from being handled by Trac, such as /stats URLs or /phpmyadmin etc. usually provided by hostings, then insert the third RewriteCond right after the second one:

RewriteCond %{REQUEST_URI} !^/(stats/|phpmyadmin) [NC]

Easy access URL as a last resort

The above configurations of Apache will not allow the user to see "cgi-bin" or "trac.cgi" string in Trac URLs. However, if you just want to automatically redirect users from your site root to trac page - put a redirect in /usr/share/trac/htdocs/index.html, such as:

<META HTTP-EQUIV="Refresh" CONTENT="0; URL=/cgi-bin/trac.cgi">

Or put the following in /usr/share/trac/htdocs/index.php if your server supports PHP to enable a transparent redirect: <?php header("Location: /cgi-bin/trac.cgi"); ?>

See also: TracInstall, TracMultipleProjects, TracFaq: "How can I create a nice URL to access trac.cgi?"

Last modified 3 years ago Last modified on Feb 18, 2015, 5:37:59 PM
Note: See TracWiki for help on using the wiki.