I want to create a simple "secret" mod_rewrite redirect (not shown in user's browser address box) to redirect:

http://www.mydomain.com/code -> http://www.mydomain.com/?utm_source=redirect&utm_medium=link&utm_campaign=code

And if possible also add www. if missing, so:

http://mydomain.com/code -> http://www.mydomain.com/?utm_source=redirect&utm_medium=link&utm_campaign=code

I tried:

# Custom rewrites
RewriteRule ^code/?$ /?utm_source=redirect&utm_medium=link&utm_campaign=code [L,NC,QSA]

My .htaccess mod_rewrite other rewrite rules look like this:

# redirect to www form
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^([a-z]+)\.%{HTTP_HOST} [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,QSA]
# rewrite file to file.php, ignore directories, ignore existing non-php files
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L,QSA]

I tried to put the custom rewrites section either in front of all the other rules or in the middle or remove [QSA]/[NC], but the problems are that it either

  • Doesn't rewrite secretly
  • Doesn't show rewrites in Google Analytics (I'm sensing it's because it does some other rewrite before and therefore Google Analytics won't count the campaign)

What am I doing wrong?

  • Google Analytics is JavaScript code that looks at the URL that the user sees. If you try too keep this information secret, then Analytics won't be able to see it either. Jan 15, 2014 at 3:40

1 Answer 1


It looks like it is not possible to do the redirect using mod_rewrite as it is processed before the page even starts loading and Google Analytics is JS which is fired only after the page starts loading.

BUT it is possible to create a JS redirect called after Google Analytics pageview has been tracked.

I have found more about that at this article: http://cfsimplicity.com/61/removing-analytics-clutter-from-campaign-urls

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.