Google analytics is very useful for tracking hits to a site for free. I set it up for my blog to see how it worked but I wanted to track hits to www.rectorsquid.com and blog.rectorsquid.com at the same time. After searching around the web and reading various forum topics about this, I just set a Google Analytics filter according to someone else’s instructions. Note that there is also a change in the Google Analytics script code in each of my pages that I am not showing here.

image

Google Analytics Subdomain Filter

I’m not sure why I need this but I think that it is just to separate the content pages in the content view. The problem is that the content view then looks like this:

image

Google Analytics Content Information

What is not obvious is that the subdomain is being displayed as part of the path and that the “http://www.rectorsquid.com” is assumed. In fact, the little icon to the right of the name is a link to the page and that link starts with “http://www.rectorsquid.com” and this is the problem I am trying to solve. My site cannot handle a link like “http://www.rectorsquid.com/blog.rectorsquid.com/preview-local-images-before-upload/” and I have found no way around this until now…

mod_rewrite

The mod_rewrite feature of Apache is a search and replace feature, to state it as simply as possible, and it can be used to fix this problem.

The mod-rewrite directive are added to the .htaccess file in the root directory for www.rectorsquid.com. My web service provider, 1&1, allows this.

The .htaccess file that I am using looks like the file below and fixes the problem by simply rewriting the URI. One interesting thing to note is that I already use a subdomain-to-path translation provided by 1&1 for “blog.rectorsquid.com” and the only time that name appears in a URI is when I am using Google Analytics. “blog.rectorsquid.com” gets converted to “www.rectorsquid.com/daveblog” before the mod_rewrite information is parsed in the .htaccess file.

RewriteEngine on
RewriteBase /
RewriteRule blog.rectorsquid.com/(.*) http://blog.rectorsquid.com/$1 [R=301,L]

Adding this simple text to the .htaccess file fixes things so that a path like “http://www.rectorsquid.com/blog.rectorsquid.com/preview-local-images-before-upload/” works and gets me to the blog page I expect.

I believe that I could change my filter to exclude the host name but then only subdomain paths would work and paths within www.rectorsquid.com would not show up or work right in Google Analytics.

I am not an expert on mod_rewrite or Google Analytics but I am providing this information because no one else seemed to show the minimum required mod_rewrite directives to accomplish this exact change.