【问题标题】:Check if ModPagespeed is allowed and enabled for the current domain via php通过 php 检查当前域是否允许并启用 ModPagespeed
【发布时间】:2012-06-20 01:31:27
【问题描述】:

我正在尝试弄清楚如何通过 PHP 在当前域上检查 ModPagespeed 是否已启用和允许。我已经编写了以下代码,但我对这个解决方案并不完全满意。它有效,但它是一个瓦罐。

摘自带有Pagespeed filters的.htaccess(服务于不同的域):

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

摘自 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>

检查此 PHP 代码是否启用或允许 Pagespeed:

        $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;
        }

我使用 PHP 5 函数 get_headers($url) 获取 Content-Length 标头。我知道原始文件的长度为 960 字节,所以我想如果不进行优化,则在提供给客户端时 Content-Length 应该是相同的。

这种方法有其他替代方法吗?我不喜欢我在 pagespeedtest.html 上执行的补充 HTTP 请求,并且该解决方案强烈依赖于 PageSpeed 配置:如果 combine_css 过滤器将被禁用,我将得到 $pagespeed_is_active == FALSE 即使 ModPagespeed 为 On 并被允许。

替代解决方案必须通过不同的域进行验证,因为我可以在同一个 .htaccess 中禁止某些域。谢谢。

【问题讨论】:

标签: php .htaccess apache2 pagespeed mod-pagespeed


【解决方案1】:

您可以查找X-Mod-Pagespeed 标头,而不是检查Content-Length,只要该域存在“ModPageSpeed on”指令,它就会为您提供您正在寻找的答案。

除此之外,我认为您没有任何方法可以检查是否从请求中启用了 PageSpeed。 PageSpeed 的全部意义在于它作为输出过滤器运行:您生成页面,PageSpeed 将其拾取并进行优化和重写。

【讨论】:

  • 非常感谢igrigorik!我已经将 mod-pagespeed-beta 包升级到 0.10.22.4(从 0.10.21.x),现在我可以简单地检查 pagespeed 是否处于活动状态,并像这样声明 $pagespeed_is_active 变量:$pagespeed_is_active = isset($pstheaders['X-Mod-Pagespeed']);。如果通过 ModPagespeedDisallow http://example.com/pagespeedtest.html 之类的请求不允许 ModPagespeed,则 $pagespeed_is_active 将为 FALSE。
猜你喜欢
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-13
  • 2013-12-28
相关资源
最近更新 更多