Edgewall Software

Version 12 (modified by techtonik <techtonik@…>, 16 years ago) ( diff )

+ another way to make pretty URL via .htaccess

Prettier URLs for Trac

If you would like the url to be like:

http://<yourhostname>/

or (maybe with a subdomain):

http://<subdomain>.<yourhostname>/

there are several ways to do this on 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: (if you have subdomain, inside a VirtualHost)

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

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

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

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]

Two RewriteCond strings tell Apache do not fire redirection to Trac script if there is existing file or directory with requested name. The last rule works always and redirects root site requests to 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 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?"

Note: See TracWiki for help on using the wiki.