Hack to get rid of index.cgi and index.fcgi from Trac 0.11 URLs

Here’s an easy hack to get rid of the index.cgi and/or index.fcgi from Trac 0.11 URLs and make them more SEO friendly. This one saves you from the trouble of hacking the source code as well, compared to a few other solutions.

I am hosting multiple Trac sites at http://trac.mydomain.com, for each project and I wanted the URLs to appear as http://trac.mydomain.com/project1/wikihttp://trac.mydomain.com/project2/wiki etc.

For this, I have pointed my trac.mydomain.com to a directory in my hosting file system, with the following contents:

total 12
drwxr-xr-x 2 myuser myuser 4096 Dec  3 12:08 cgi-bin
-rw-r--r-- 1 myuser myuser   18 Dec  7 01:40 index.php
-rwxr-xr-x 1 myuser myuser  282 Dec  7 03:02 project1
-rwxr-xr-x 1 myuser myuser  282 Dec  7 03:02 project2
-rw-r--r-- 1 myuser myuser   90 Dec  7 03:13 .htaccess

So there are two files corresponding to two hosted projects, namely project1 and project2, both executable with the following content (replace the {} variables with your choices):

#!/bin/bash

export HOME="{full/path/to/your/home/directory}"
export TRAC_ENV="$HOME/trac_sites/{project-name}"
export PYTHONPATH="$HOME/packages/lib/python2.3/site-packages"
export PATH="$HOME/packages/bin:$PATH"
export LD_LIBRARY_PATH="$HOME/packages/lib"

exec $HOME/packages/share/trac/cgi-bin/trac.fcgi

The key is the .htaccess file below, that does the routing.

Options +FollowSymLinks +ExecCGI
<Files ~ '^[^.]+$'>
     SetHandler fcgid-script
</Files>

We’re assigning the processing of all files in the directory without an extension an extension to FastCGI handler.

So what happens here: When a request is made to http://trac.mydomain.com/project1, our .htaccess file will assign FastCGI handler to project1 file and you’ll get Trac homepage at the URL. Since project1 is the script name here instead of index.cgi or index.fcgi, Trac 0.11 will continue to use project1 in the URL, resulting in URLs like:http://trac.mydomain.com/project1/wikihttp://trac.mydomain.com/project1/timeline etc.

Tags: ,

Copyright (c) 2000-2011 Alex N J. All rights reserved.
All and any opinion expressed here are solely mine and do not represent the views of my employer or any other person or organization related to me. Entries (RSS) and Comments (RSS).