When users come directly to domain.com or www.domain.com, I am redirecting them to shop.domain.com via this .htaccess rewrite:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain.com$ 
RewriteRule ^(.*)$ http://shop.domain.com/ [R=301,L]

The content served by shop.domain.com has the following tracking code parameters:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456-6']);
_gaq.push(['_setDomainName', '.domain.com']);

All direct visits that come to shop.domain.com as a result of the rewrite from domain.com are tracked as referral traffic, showing my own domain.com as referral source in Google Amalytics.

I would like to track these visits as direct traffic.

How to change the configuration to track mod_rewritten traffic on my subdomain coming from my own domain as direct traffic?

  • Has your question been answered here? Feb 14, 2011 at 19:50

3 Answers 3


Instead of overriding the referrer information, you should add the subdomain to the Referrer Ignore List that Google Analytics stores.

You can do this using the _addIgnoredRef() method in the GA API.

Google's description of the the method:


Excludes a source as a referring site. Use this option when you want to set certain referring links as direct traffic, rather than as referring sites. For example, your company might own another domain that you want to track as direct traffic so that it does not show up on the "Referring Sites" reports. Requests from excluded referrals are still counted in your overall page view count.


Note: the examples given by Google may not be 100% correct. Check out the following link for a detailed discussion and example implementation:



You can override the referrer that Google Analytics is using, read more in the documentation for _setReferrerOverride.

To somehow provide this correct referrer, you need to pass it via the 301 redirect. I can suggest adding a query parameter to your redirected url, and writing a javascript snippet that will extract this information from the document.location.

An example of adding javascript to your GA code:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
var _from = unescape(window.location.href.replace(/.*[?&]from=(.*)/,'$1'));
if (_from && _from.length > 0) {

And, an example of using mod_rewrite to transfer the referrer, if a referrer exists:

RewriteCond %{HTTP_HOST} ^domain.com\.com$ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} (.*)
RewriteRule ^(.*)$ http://shop.domain.com/$1?from=%1 [R=301,QSA,L]

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://shop.domain.com/$1 [R=301,L]

You will need to repeat the above for www.domain.com as well.

  • disclaimer: even though I tried to test this as best as I can, it may still not work 100%. so I leave the testing up to the users of the suggested above. Oct 30, 2010 at 23:58
  • Thank you for the suggestion Evgeny. Even though this seems to work, I would not like to add URL parameters when performing the redirection. I would prefer a method that would not alter user experience in any way.
    – Marek
    Oct 31, 2010 at 19:59
  • well, you can improve the above be redirecting to a url with a hash (#) instead of a query param. and you can remove hashes using javascript without the page reloading in the browser. Oct 31, 2010 at 20:03

The following example describes how to keep the referrer while switching from http to https.

In your .htaccess:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1?referrer=%{HTTP_REFERER} [L,QSA,R=301]

The Google Analytics call:

(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
ga('create', 'UA-xxx-y', 'auto');
ga('set', 'anonymizeIp', true);
try {
  var match = /.*[?&]referrer=(.*)/.exec(window.location.href);
  if (match && match.length > 1) {  
      var _from = decodeURIComponent(match[1]);
      if (_from && _from.length > 0) {
          console.log('Override referrer:', _from);
          ga('set', 'referrer', _from);
} catch (err) { ; }
ga('send', 'pageview');
  • 1
    There should be no space in the RewriteRule flags. ie. [L, QSA,R=301] should be [L,QSA,R=301] (otherwise, you'll get a 500 error).
    – MrWhite
    Aug 2, 2017 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.