如何获取mod_rewrite重定向的分析统计信息?

时间:2014-03-19 18:11:12

标签: apache mod-rewrite redirect google-analytics webserver

我在httpd.conf中有一个大约200个apache mod_rewrite规则的网站,在redhat的apache webserver上运行。

以下是其中一条规则的示例,其中大多数都是重定向到真正长网址的短网址:

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

我被要求为这些重定向获取一些网络分析。

"有多少人使用了网址mysite.com/grad2014?" - 好吧,我真的不知道,因为/ grad2014在网络服务器上不存在,并且在index.html页面上设置了谷歌分析。

我似乎没有看到access.log中的任何快捷方式。有没有其他方法可以查看哪些网址重定向最受欢迎?有没有办法开始记录这个?

2 个答案:

答案 0 :(得分:5)

你可以采取一种方式:

在目标网址上添加重写匹配作为查询参数:

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

然后在您的GA代码中,捕获该URL参数并将其放入自定义变量中。我不知道你是否对custom variables有任何了解,但这里有一个设置它的方法的例子:

// 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"]);

注意:默认情况下,GA会将网页名称计为location.pathname+location.search,因此将rr参数添加到网址会影响您的网页报告。解决此问题的最简单方法是在GA中创建一个过滤器,以便从传入的页面名称(请求URI)中删除它,但是如果需要,可以编写一些代码来获取location.pathname+location.search减去rr个查询param并将该值填充为_trackPageview中的第二个元素。

_gaq.push(["_trackPageview","custom page name here"]);

答案 1 :(得分:3)

access.log应该为您提供所需的内容,例如

cd /var/log/apache2 || cd /var/log/httpd

# Get a list of the 301 redirects issued.
grep 301 access?log


# Count the 301 redirects issued.
grep 301 access?log | wc -l

# Count a specific redirect
grep 301 access?log | grep grad2014 | wc -l