Tracking source URL with Google Analytic when redirecting by .htaccess

I got a situation like client have some URLs but the content or path doesn’t exists. So, clients need to redirect these URLs to other page. In addition he wanted to know, does the user came directly to that page or came through redirecting from those sources. So, I found a solution that worked for me and I liked to share it with you guys so that it solves your needs if your in the same situation. As the destination page already had the GA codes, I went through .htaccess solution. I just added two line codes in the .htaccess file as below: Consider http://yourdomain.com/abc.php is the source and http://yourdomain.com/xyz.php is the destination.

 
RewriteCond %{REQUEST_URI} ^/abc.php   
RewriteRule ^abc.php(.*)$ http://yourdomain.com/xyz.php?utm_source=redirects&utm_medium=abc.php&utm_campaign=301_Redirects [L,R=301]
 

So three things you need to add:

  1. utm_source=redirects
  2. utm_medium=abc.php (which is the source url)
  3. utm_campaign=301_Redirects

Now browse the source URL and you will be redirected to destination page. And then go to your GA account and see in real time view, you will found this URL is listed as redirect source.

I am open to answer any question…