Making stats accessible with htaccess

Introduction
Many CMS and blog tools install a .htaccess file that makes our stats unreachable, including our WordPress and Joomla one-click installs and the Drupal and Textpattern CMS systems. This problem does not apply to a WordPress blog installed in a sub directory of your server. (For example, a blog at domain.com/ will be affected, but a blog at domain.com/blog/ will not be.)

To correct this situation, you will need to edit your .htaccess file. For these examples we will use a WordPress .htaccess file. (Drupal instructions.)

Editing an .htaccess file
An .htaccess file is a plain text file. As such, there are many different ways to edit the file. For some, the easiest way to edit an .htaccess file is to go to the DreamHost Web Panel, click on "Domains" then click on "Manage Domains" and then click on "WebFTP" under the domain name that you want to edit the .htaccess file of. On the next page click on "Edit" under Actions in the .htaccess file row. Edit the .htaccess file in the page that opens and remember to click on the blue floppy disk icon to save the edited file.

Note: While this works well enough with .htaccess files, it will *not* work with other scripts, as the editor in the "WebFTP" application saves the file with DOS style line endings, which will break many scripts (particularly perl scripts!).

'''UPDATE 01-11-2012: The WebFTP app is now replaced with AjaXplorer and as of this date does not handle hidden files, such as .htaccess. So you will have to download the .htaccess file using a real FTP client, make the changes on your computer, save it, and upload the file back to your server.'''

A better way to edit an .htaccess file is to log into the shell and open the file up in pico, a Unix/Linux based text editor,make your changes, and re-save it. To do so, type this into the command prompt while in the same directory as the .htaccess file:

pico .htaccess

This will open up the file in pico. Now, make your changes (see the next section). Then, quit pico and save when prompted by typing control-X. When pico saves files, it strips them of carriage returns.

In the long term, you may want find a text editor, which you can use to edit files on your own computer, that gives you an option to save with 'Unix-style' line endings. This enables you to use FTP to copy the .htaccess file to your computer, edit it with familiar tools, and then place it back onto the server (again via FTP).

For the Macintosh, Bare Bones BBEdit (or the free Textwrangler) is regarded as the best text editor to use.

http://www.barebones.com/

For Windows-based PCs, a popular text editor that many use is PSPad. A free version is available here:

http://www.pspad.com/

Good text editors are available for just about any platform. Many are "free" and/or open source, and can be easily located via a Google search.

What To Change
Your "standard" WordPress .htaccess file will look something like this (since WordPress 3.0 according to the codex):

 RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule. /index.php [L] 
 * 1) BEGIN WordPress
 * 1) END WordPress

To allow the "stats" directory to be displayed properly on Dreamhost, you need to add the following lines to your .htaccess before the section added by WordPress:

ErrorDocument 401 "Error" ErrorDocument 403 "Error"  RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$ [NC] RewriteRule. - [L] 

Those lines are self contained, so you can put them at the beginning of any .htaccess file to fix stats, they won't interfere with other .htaccess operations.

The above entries work with Drupal as well (added above your standard Drupal .htaccess file.) [D v6.10]

The final .htaccess file for WordPress would look like:

ErrorDocument 401 "Error" ErrorDocument 403 "Error"  RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$ [NC] RewriteRule. - [L]   RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule. /index.php [L] 
 * 1) BEGIN WordPress
 * 1) END WordPress

The repeated lines won't cause any problems, and are necesary because WordPress 2.0+ has taken to doing whatever it pleases to the stuff within the WordPress section.

If this doesn't seem to initially work for you, remember to clear your cache ( shift + refresh )

The same basic idea works for Textpattern. Here's a sample .htaccess for Textpattern.  RewriteEngine On  # Dreamhost stats RewriteBase / RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$ [NC] RewriteRule. - [L] # Textpattern RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^(.+) - [PT,L] RewriteRule. /index.php 

To get your stats working in Ruby on Rails or Joomla add the following to the top of the .htaccess file (located in the "public" directory of your Rails App or root directory of your Joomla web site) to get stats working again. Unfortunately, if you are using Phusion Passenger to deploy your Ruby on Rails application, then stats will not be available from the Web because Passenger overrides the behavior of the .htaccess file. It is recommended that you use a third-party stats package, or use the alternative text mode method, below.

 RewriteEngine On  RewriteBase / RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$ [NC] RewriteRule. - [L] 

OR

Alternative text mode method
An alternative method to view your stats is via a ssh/telnet terminal session. This is if you don't want to change your .htaccess file to handle the /stats/ subdirectory and can open a console to your domain.

i.e. putty / ssh into yourdomain.com.

Change to the ~/logs/yourdomain.com/http/html directory.

Then open the index.html page in a terminal browser. (e.g. links, lynx, or w3m)

Example session: ssh user@yourdomain.com cd logs/yourdomain.com/http/html links index.html

Examples
More details of what exactly this "stats" thing is.


 * Accessing Stats
 * Nginx - Making stats accessible when using wordpress
 * "Accessing Website Statistics On a DreamHost Server"
 * "Where can I view my stats?"
 * "How to fix my site statistics problem on a WordPress 2.x blog hosted at Dreamhost?"
 * "Get The Dreamhost Stats Page Working on a WordPress Site"
 * "Stats page not working"