Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get analytics stats for mod_rewrite redirects?

I have a website with about 200 apache mod_rewrite rules in httpd.conf, running on apache webserver in redhat.

Here's an example of one of the rules, most of them are short URLs that redirect to really long URLs:

 RewriteRule ^grad2014/?$ /registration-and-records/graduation/live/index.html [R=301,L]

I've been asked to get some web analytics for these redirects.

"How many people used the URL mysite.com/grad2014?" - Well, I don't really know, because /grad2014 doesn't exist on the webserver, and google analytics are set up on the index.html page.

I don't seem to see any of the shortcuts in the access.log. Is there another way to see which URLs redirects are the most popular ? Is there a way to start logging this ?

like image 846
jeph perro Avatar asked Jan 23 '26 11:01

jeph perro


1 Answers

One way you could do it:

Add the rewrite match as a query param on the target url:

RewriteRule ^grad2014/?$ /registration-and-records/graduation/live/index.html?rr=$0 [R=301,L]

Then in your GA code, capture that URL param and put it in a custom variable. I don't know if you know anything about custom variables, but here is an example of one way to set it:

// example function get query param. use your own if you already have one
function getParam(n){var n=n||'';var x=new RegExp("[\\?&]"+n.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]")+"=([^&#]*)");var r=x.exec(window.location.href);return(r==null)?'':r[1]}

var rr = getParam('rr');
if (rr) {
  _gaq.push(["_setCustomVar", 1, "Mod Rewrite Redirect URL", rr, 3]);
}
// your on-page trigger
_gaq.push(["_trackPageview"]);

NOTE: by default GA counts the page name as location.pathname+location.search so adding the rr param to the url is going to affect your pages report. The easiest way to fix this is to create a filter within GA to strip it from incoming page name (request URI), but if you need to, you can write some code to get location.pathname+location.search minus rr query param and populate that value as the 2nd element in your _trackPageview.

_gaq.push(["_trackPageview","custom page name here"]);
like image 162
Crayon Violent Avatar answered Jan 25 '26 06:01

Crayon Violent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!