dongyizhuang0134 2016-08-15 11:55
浏览 66
已采纳

VirtualHost模块在交付给客户之前搜索和替换HTML网站代码

I want to replace inside VirtualHost </body> element with:

<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//stats.mydomain.com/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', '1']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<noscript><p><img src="//stats.mydomain.com/piwik.php?idsite=1" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code --></body>

I need to do this inside VirtualHost section because I can't directly modify website code.

I was thinking about some apache modules like mod_substitute, mod_sed, mod_include.

I need to add this PIWIK statistics code into every page.

Is this good way? Which one of this mods will be best to do this? Or maybe there is another solution?

  • 写回答

1条回答 默认 最新

  • doulang2311 2016-08-16 19:35
    关注

    Below is solution with mod_ext_filter which looks very powerful:

    1. Enable Apache module:

      a2enmod ext_filter
      
    2. Inside Apache configuration file, within VirtualHost section, add filter definition:

      ExtFilterDefine my_html_filter mode=output intype=text/html cmd="/usr/bin/php5 /var/www/data/myfilter.php"
      SetOutputFilter my_html_filter
      
      • ExtFilterDefine - this directive defines filter
      • my_html_filter - it's our filter name
      • mode=output - tells Apache to process the response
      • intype=text/html - specifies the MIME type of documents which should be filtered
      • cmd="/usr/bin/php5 /var/www/data/myfilter.php" - specifies the external command to run
      • SetOutputFilter - this directive activates filter
    3. Content of PHP file /var/www/data/myfilter.php:

      <?php
      
      $stdin = fopen('php://stdin', 'r');
      while($line = fgets($stdin)){
       $line = str_replace('</body>',"
      <!-- Piwik -->
      <script type='text/javascript'>
        var _paq = _paq || [];
        _paq.push(['trackPageView']);
        _paq.push(['enableLinkTracking']);
        (function() {
          var u='//stats.mydomain.com/';
          _paq.push(['setTrackerUrl', u+'piwik.php']);
          _paq.push(['setSiteId', '1']);
          var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
          g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
        })();
      </script>
      <noscript><p><img src='//stats.mydomain.com/piwik.php?idsite=1' style='border:0;' alt='' /></p></noscript>
      <!-- End Piwik Code -->
      </body>", $line);
      
       echo $line;
      }
      fclose( $stdin );
      
      ?>
      
    4. Restart Apache:

      service apache2 restart
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图