Appending Google Analytics Tracking Code When 301 Redirecting a URL

Published by John on June 26, 2018 Under Website Hosting

A local dentist I work with recently bought out another company and with it took ownership of their website. Since we had just built and launched a new website for his business, we opted to redirect the other website to his new site. However, we wanted to be able to show a popup message to people who reached the site via the redirect and also add some Google Analytics tracking to get a feel for what type of traffic he got from the other website.

Since we were doing a 301 redirect to help preserve google rankings, we opted to do a 301 redirect on the webserver. To ensure we could tell where the traffic was coming from, we wanted to append the following to each URL when redirecting: ?utm_source=redirect&utm_medium=web&utm_name=some_name

Ultimately, this can be done via two lines of code in your htaccess or the <VirtualHost> directive in your httpd.conf/virtual.conf settings:

  RewriteEngine  on
  RewriteRule "^/?(.*)" "https://new_url.com/$1?utm_source=redirect&utm_medium=web&utm_name=some_name" [L,R=301,NE]

Of course, make sure to update the new_url.com and all of the utm_ variables with the correct settings for your server.

Also, keep in mind that this will probably not work if you have an existing $_GET variable in the URL, but worked fine for our purposes.


No Comments |

Add a Comment