Question

i'm trying to figure out how to check if ModPagespeed is enabled and allowed on the current domain via PHP. I've written the following code, but I'm not fully satisfied of this solution. It works, but it's a crock.

Excerpt from .htaccess (serving different domains) with Pagespeed filters:

<IfModule pagespeed_module>
  ModPagespeed On
  ModPagespeedRewriteLevel PassThrough
  ...
  ModPagespeedEnableFilters combine_css
  ...
</IfModule>

Excerpt from pagespeedtest.html:

...
<head>
...
  <!-- If combine_css filter is active, the following code should be combined -->
  <style type="text/css" media="all">@import url("/style1.css");</style>
  <style type="text/css" media="all">@import url("/style2.css");</style>
  <style type="text/css" media="all">@import url("/style3.css");</style>
...
</head>

Check if Pagespeed is enabled or allowed by this PHP code:

        $psturl = 'http://'. $_SERVER['HTTP_HOST'] .'/pagespeedtest.html';
        $pagespeed_is_active = TRUE;
        $pstheaders = get_headers($psturl, 1);
        if ($pstheaders['Content-Length'] == 960) {
          $pagespeed_is_active = FALSE;
        }

I get the Content-Length header using the PHP 5 function get_headers($url). I know the original file is 960 bytes long so I suppose that if no optimization is performed the Content-Length should be the same when served to the client.

Are there any alternatives to this approach? I don't like the supplemental HTTP request I perform on pagespeedtest.html and that solution strongly relies on PageSpeed configuration: if combine_css filter will be disabled, I will got $pagespeed_is_active == FALSE even when ModPagespeed is On and allowed.

An alternative solution must be validated through different domains, since I can disallow some domains in this very same .htaccess. Thank you.

Était-ce utile?

La solution

Instead of checking the Content-Length, you can instead look for the X-Mod-Pagespeed header, which will give you the answer you're looking for as long as "ModPageSpeed on" directive is there for the domain.

Other then that, I don't think there is any way for you to check if PageSpeed is enabled from within the request. The whole point of PageSpeed is that it runs as an output filter: you generate your page, PageSpeed picks it up and does the optimization and rewriting.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top