最好的办法COM preSS HTML,CSS和放大器;与mod_deflate模块和JS MOD_GZIP禁用 [英] Best way to compress HTML, CSS & JS with mod_deflate and mod_gzip disabled

查看:122
本文介绍了最好的办法COM preSS HTML,CSS和放大器;与mod_deflate模块和JS MOD_GZIP禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个共享的主机上的几个网站运行的Apache 2,我想COM preSS被输送到浏览器的HTML,CSS和Javascript。主机已禁用mod_deflate模块和mod_gzip的,所以这些选项都出来了。我确实有PHP 5在我手上,不过,这样我就可以使用了gzip的组成部分。

我目前把我的.htaccess文件如下:


  

php_value output_handler ob_gzhandler


不过,这仅COM presses HTML和省去了CSS和JS。

有透明地融为一体pressing CSS和JS的输出,而无需改变每一页一种可靠的方法?我已搜查谷歌和一些解决方案都是presented,但我还没有得到一个工作。如果任何人都可以表明,他们知道工作的一个解决方案,那将是非常感激地接受。

请注意,方法2 权威邮报gzip压缩你的CSS 看起来像一个很好的解决方案,但我无法得到它的工作。有没有人用这种方法成功地别的?


解决方案

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

假设:


  • 的.htaccess 是在同一个文件为 COM press.php

  • 静态文件是COM pressed在静态子目录

我开始在.htaccess中设置以下指令我的解决方法:

  RewriteEngine叙述上
重写规则^静态/.+ \\。(JS | ICO | GIF | JPG | JPEG | PNG | CSS | SWF)$ COM press.php [NC]

它要求你的供应商,您可以覆盖的.htaccess 文件的mod_rewrite 选项。
然后,COM press.php文件本身可以是这样的:

 < PHP$ BASEDIR =真实路径(目录名($ _ SERVER ['SCRIPT_FILENAME']));
$文件的真实路径=($ BASEDIR $ _ SERVER [REQUEST_URI]);如果(file_exists($文件)及!&安培; strpos($文件,$ BASEDIR)=== 0){
    标题(HTTP / 1.0 404未找​​到);
    打印文件不存在。
    出口();
}$组件=拆分('\\',基本名($文件));
$延长=用strtolower(array_pop($部件));开关($扩展)
{
    案CSS:
    $哑剧=文/ CSS
    突破;
    默认:
    $哑剧=text / plain的;
}标题(内容类型:$ MIME);
ReadFile的($文件);

您当然应该增加更多的MIME类型switch语句。我不想让解决方案依赖于PECL 的FileInfo 扩展或任何其他神奇的MIME类型检测库 - 这是最简单的做法

至于固定脚本 - 我做一个翻译文件系统所以没有破解'../../../etc/passwd或其他shellscript里文件路径不通过一个真实路径<。 / p>

这就是

  $ BASEDIR =真实路径(目录名($ _ SERVER ['SCRIPT_FILENAME']));
$文件的真实路径=($ BASEDIR $ _ SERVER [REQUEST_URI]);

片段。虽然我pretty肯定大部分都是超过$ BASEDIR将被Apache处理得到他们甚至达到脚本之前,其他层次的路径。

此外,我检查的路径是脚本的目录树中。
添加页眉高速缓存控制,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.

这篇关于最好的办法COM preSS HTML,CSS和放大器;与mod_deflate模块和JS MOD_GZIP禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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