Making stats accessible with htaccess

From DreamHost
Jump to: navigation, search

Overview

Many CMS and blog applications generate their own .htaccess file that makes DreamHost stats unreachable. These include applications such as One-Click Installs WordPress, Joomla, and Textpattern. Drupal also produces its own .htaccess file as well. This problem does not apply to a WordPress blog installed in a subdirectory on your server. For example, a blog at example.com/ will be affected, but a blog at example.com/blog/ will not be.

To correct this situation, you will need to edit your .htaccess file.

Editing an .htaccess file

An .htaccess file is a plain text file. As such, there are many different ways to edit the file. The easiest way is to use an FTP client. In that client, make sure the ability to view hidden files is enabled. This allows you to see files beginning with a period which you can then edit and save by using your client.

A better way to edit an .htaccess file is to connect to your server via SSH and open the file up in a text editor. A good choice is a text editor called ‘nano’. You can then simply make your changes, and re-save it.

For example, run this in your site’s directory:

nano .htaccess

This opens the file to edit. When finished editing, click CTRL + X and ENTER to save.

In the long term, you may want to find a text editor that you can use to edit files on your own computer, which 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:

For Windows-based PCs, a popular text editor that many use is Notepad++:

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 in the .htaccess file

Your "standard" WordPress .htaccess file looks something like this (since WordPress 3.0 according to the codex):

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# 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"

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$ [NC]
RewriteRule . - [L]
</IfModule> 
  • The lines used in the above example are self contained, so you can put them at the beginning of any .htaccess file to fix stats and won't interfere with other .htaccess operations.
  • The above entries work with Drupal as well (added above your standard Drupal .htaccess file).

The final .htaccess file for WordPress would look like:

ErrorDocument 401 "Error"
ErrorDocument 403 "Error"

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

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

If this doesn't seem to initially work for you, remember to clear your cache. Visith the following article for further details:

The same basic idea works for Textpattern. Here's a sample .htaccess for Textpattern.

<IfModule mod_rewrite.c>
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
</IfModule>

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):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$ [NC]
RewriteRule . - [L]
</IfModule>
Note2 icon.png Note: Unfortunately, if you are using Phusion Passenger to deploy your Ruby on Rails application, 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.


See also