Run Trac alongside another CMS

Recently I was setting up SVN and Trac on one of my domains. The domain was already running Drupal in the root directory (accessible at mysite.com), and the Trac installer was configured to place trac in mysite.com/trac. However, Drupal was picking up all web requests for /trac and redirecting them to a 404 error page. I found out that some small tweaks needed to be made to the Drupal-provided .htaccess file at the root of my site.

How to Fix 404 error when Trac is in a
subdirectory of another CMS

Note: This should work with many CMS/frameworks such as Drupal, WordPress, CakePHP, etc.

The file to edit is .htaccess, which should be in the root of your CMS installation. Make a backup before modifying this file!

Open the file and look for the section beginning with [crayon nums=”false” toolbar=”false”]RewriteEngine On[/crayon]

There should be one or more RewriteCond statements followed by a RewriteRule. On a new line right before the RewriteRule, add the following (assumes Trac is located in /trac)
[crayon nums=”false” toolbar=”false”]RewriteCond %{REQUEST_URI} !^/trac*[/crayon]
This line tells the rewrite engine to ignore any requests for /trac.

Here is the standard Drupal (6) .htaccess file (starting at line 73) with the line above added in and highlighted.

[crayon start-line=”73″ marking=”true” mark=”35″ toolbar=”false”]
# Various rewrite rules.

RewriteEngine on

# If your site can be accessed both with and without the ‘www.’ prefix, you
# can use one of the following settings to redirect users to your preferred
# URL, either WITH or WITHOUT the ‘www.’ prefix. Choose ONLY one option:
#
# To redirect all users to access the site WITH the ‘www.’ prefix,
# (http://example.com/… will be redirected to http://www.example.com/…)
# adapt and uncomment the following:
# RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
# RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
#
# To redirect all users to access the site WITHOUT the ‘www.’ prefix,
# (http://www.example.com/… will be redirected to http://example.com/…)
# uncomment and adapt the following:
# RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
# RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

# Modify the RewriteBase if you are using Drupal in a subdirectory or in a
# VirtualDocumentRoot and the rewrite rules are not working properly.
# For example if your site is at http://example.com/drupal uncomment and
# modify the following line:
# RewriteBase /drupal
#
# If your site is running in a VirtualDocumentRoot at http://example.com/,
# uncomment the following line:
# RewriteBase /

# Rewrite URLs of the form ‘x’ to the form ‘index.php?q=x’.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{REQUEST_URI} !^/trac*
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
[/crayon]

We also have to add in a line of code to allow Trac to handle basic http authentication. At the top of .htaccess, below
[crayon nums=”false” toolbar=”false”]
# Make Drupal handle any 404 errors.
ErrorDocument 404 /index.php
[/crayon]

add the following code:
[crayon nums=”false” toolbar=”false”]
# Make Trac handle 401 errors
ErrorDocument 401 /401.html
[/crayon]

You then need to create the file 401.html in your root directory. It can be empty, or you can add a message letting the user know that authentication is required.

Voila! Trac now works in a subdomain of your existing CMS.

Important note: Changes to .htaccess will probably be wiped out on any subsequent update to the CMS. Make sure you keep a backup copy once Trac is working, or just follow these instructions after each update.

References: