Enable Statistics for Mod Pagespeed with WordPress

I found out that mod pagespeed statistics are not working properly when using WordPress on the domain. Usually the /mod_pagespeed_statistics is accessible but in certain cases it will only show a Forbidden or a 404 not found page when trying to access the statistics page for PageSpeed. After a bunch of searching I found where the problem lies.

google pagespeed website header
Google Pagespeed Website Header

In my pagespeed.conf it is set to:

# Enables server-side instrumentation and statistics. If this rewriter is
# enabled, then each rewritten HTML page will have instrumentation javacript
# added that sends latency beacons to /mod_pagespeed_beacon. These
# statistics can be accessed at /mod_pagespeed_statistics. You must also
# enable the mod_pagespeed_statistics and mod_pagespeed_beacon handlers
# below

ModPagespeedEnableFilters add_instrumentation

# This handles the client-side instrumentation callbacks which are injected
# by the add_instrumentation filter.
# You can use a different location by adding the ModPagespeedBeaconUrl
# directive; see the documentation on add_instrumentation.
SetHandler mod_pagespeed_beacon

# Uncomment of the following line if you want to disable statistics entirely.
ModPagespeedStatistics on

# This page lets you view statistics about the mod_pagespeed module.
Order allow,deny
# You may insert other "Allow from" lines to add hosts you want to
# allow to look at generated statistics. Another possibility is
# to comment out the "Order" and "Allow" options from the config
# file, to allow any client that can reach your server to examine
# statistics. This might be appropriate in an experimental setup or
# if the Apache server is protected by a reverse proxy that will
# filter URLs in some fashion.
Allow from localhost
Allow from xxx.xxx.xxx.xxx
SetHandler mod_pagespeed_statistics&lt;/location&gt;

When I go to mydomain/mod_pagespeed_statistics I get a 404 not found error page.

404 error page
404 error page

This is what I needed to do:

In the WordPress default rewrite rules you need to add:

RewriteCond %{REQUEST_URI} !^/mod_pagespeed_[a-z_]+$

So my rules look like this:

#BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_URI} !^/mod_pagespeed_[a-z_]+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
# END WordPress

Remember these are my rules so yours could be different like the 301 redirect etc.

Now I am able to view the mod pagespeed statistics page, all though there is another problem with the refresh but that is another issue ;)

ref: Pagespeed issue 597

Leave a Reply

Your email address will not be published. Required fields are marked *