最佳方式压缩HTML,CSS& JS禁用mod_deflate和mod_gzip [英] Best way to compress HTML, CSS & JS with mod_deflate and mod_gzip disabled

查看:133
本文介绍了最佳方式压缩HTML,CSS& JS禁用mod_deflate和mod_gzip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行Apache 2的共享主机上有几个网站。我想压缩传送到浏览器的HTML,CSS和Javascript。主机禁用了mod_deflate和mod_gzip,所以这些选项都没有了。我现在有PHP 5,但我可以使用gzip组件。



我目前在我的.htaccess文件中放置以下内容:


php_value output_handler ob_gzhandler


压缩HTML并抛弃CSS和JS。



有没有可靠的方法透明地压缩CSS和JS的输出,而不必改变每一页?我已经搜索过谷歌,并提出了一些解决方案,但我还没有得到一个工作。



请注意,方法2 a href =http://www.fiftyfoureleven.com/weblog/web-development/css/the-definitive-css-gzip-method =nofollow noreferrer> GZipping您的CSS的最终文章 看起来像一个很好的解决方案,但我不能得到它的工作。有没有人成功使用此方法?

解决方案

抱歉的延迟 - 这对我来说是一个忙碌的一周。 b
$ b

假设:




  • .htaccess 在与 compress.php

  • 相同的文件中,要压缩的静态文件位于 static 子目录



我通过在.htaccess中设置以下指令开始了我的解决方案:

  RewriteEngine on 
RewriteRule ^ static /.+ \。(js | ico | gif | jpg | jpeg | png | css | swf)$ compress.php [NC]

要求您的提供程序允许您覆盖 mod_rewrite .htaccess 文件中的code>选项。
那么compress.php文件本身可以看起来像这样:

 <?php 

$ basedir = realpath(dirname($ _ SERVER ['SCRIPT_FILENAME']));
$ file = realpath($ basedir。$ _SERVER [REQUEST_URI]);

if(!file_exists($ file)&&&strpos($ file,$ basedir)=== 0){
header(HTTP / 1.0 404 Not Found);
print文件不存在。
exit();
}

$ components = split('\。',basename($ file));
$ extension = strtolower(array_pop($ components));

switch($ extension)
{
case'css':
$ mime =text / css
break;
默认值:
$ mime =text / plain;
}

header(Content-Type:。$ mime);
readfile($ file);

你当然应该在switch语句中添加更多的mime类型。我不想让解决方案依赖于pecl fileinfo 扩展或任何其他魔法mime类型检测库 - 这是最简单的方法。



对于保护脚本 - 我做一个翻译到文件系统中的真实路径,所以没有黑客的../../../etc/passwd或其他shellscript文件路径,



这是

  $ basedir = realpath dirname($ _ SERVER ['SCRIPT_FILENAME'])); 
$ file = realpath($ basedir。$ _SERVER [REQUEST_URI]);

代码段。虽然我很确定大多数路径在其他层次结构中的$ basedir将被Apache处理之前,他们甚至达到脚本。



还要检查if生成的路径在脚本的目录树中。
添加缓存控制的头像pilif建议,你应该有一个工作的解决方案你的问题。


I have a few sites on a shared host that is running Apache 2. I would like to compress the HTML, CSS and Javascript that is delivered to the browser. The host has disabled mod_deflate and mod_gzip, so these options are out. I do have PHP 5 at my disposal, though, so I could use the gzip component of that.

I am currently placing the following in my .htaccess file:

php_value output_handler ob_gzhandler

However, this only compresses the HTML and leaves out the CSS and JS.

Is there a reliable way of transparently compressing the output of the CSS and JS without having to change every page? I have searched Google and a number of solutions are presented, but I've yet to get one to work. If anyone could suggest a solution that they know to work, that would be very gratefully received.

Note, Method 2 in The Definitive Post on Gzipping your CSS looks like a good solution, but I couldn't get it working. Has anyone else succeeded using this method?

解决方案

Sorry about the delay - it's a busy week for me.

Assumptions:

  • .htaccess is in the same file as compress.php
  • static files to be compressed are in static subdirectory

I started my solution from setting the following directives in .htaccess:

RewriteEngine on
RewriteRule ^static/.+\.(js|ico|gif|jpg|jpeg|png|css|swf)$ compress.php [NC]

It's required that your provider allows you to override mod_rewrite options in .htaccess files. Then the compress.php file itself can look like this:

<?php

$basedir = realpath( dirname($_SERVER['SCRIPT_FILENAME']) );
$file = realpath( $basedir . $_SERVER["REQUEST_URI"] );

if( !file_exists($file) && strpos($file, $basedir) === 0 ) {
    header("HTTP/1.0 404 Not Found");
    print "File does not exist.";
    exit();
}

$components = split('\.', basename($file));
$extension = strtolower( array_pop($components) );

switch($extension)
{
    case 'css':
        $mime = "text/css";
        break;
    default:
        $mime = "text/plain";
}

header( "Content-Type: " . $mime );
readfile($file);

You should of course add more mime types to the switch statement. I didn't want to make the solution dependant on the pecl fileinfo extension or any other magical mime type detecting libraries - this is the simplest approach.

As for securing the script - I do a translation to a real path in the file system so no hacked '../../../etc/passwd' or other shellscript file paths don't go through.

That's the

$basedir = realpath( dirname($_SERVER['SCRIPT_FILENAME']) );
$file = realpath( $basedir . $_SERVER["REQUEST_URI"] );

snippet. Although I'm pretty sure most of the paths that are in other hierarchy than $basedir will get handled by the Apache before they even reach the script.

Also I check if the resulting path is inside the script's directory tree. Add the headers for cache control as pilif suggested and you should have a working solution to your problem.

这篇关于最佳方式压缩HTML,CSS&amp; JS禁用mod_deflate和mod_gzip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆