缩小动态 php 样式表
所以我一直在使用 Minify 来压缩我的 JS 和 CSS,直到我需要压缩一些动态 php 样式表。
我试图使用 htaccess 来愚弄它,让它认为它是一个 css 文件,但后来我意识到它使用绝对文件路径,不会受到 mod_rewrite 的影响
无论如何,每当我将它指向 php 文件时,它总是返回“400 Bad Request” 。除了编写我自己的压缩脚本之外,还有如何解决这个问题的想法吗?
So I have been using Minify to compress my JS and CSS which had all been going nicely until I needed to compress some dynamic php stylesheets.
I tried to use htaccess to fool it into thinking it was a css file, but I then realised it uses absolute file paths which would not be effected by mod_rewrite
Anyways whenever I point it at a php file, it always returns '400 Bad Request'. Any idea on how to solve this other than writing my own compression script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现处理缩小和压缩样式表的最佳方法是自己动手。查看这段代码:
这里发生了很多事情,所以让我解释一下。
标头
Last-Modified
和Expires
标头以进行缓存控制(设置在一周多一点,您可以更改此设置)。压缩和 GZIP
ob_start
,我们告诉它对该文件进行 GZIP 压缩并运行自定义函数compress
。compress
在发送到浏览器时删除所有 CSS 注释和空格。这意味着您可以在源文件中按照您喜欢的方式保留所有注释和间距,以便于编辑,但只将最少的内容发送到浏览器。样式
require
导入样式表。对任意数量的样式表执行此操作;它们都将通过单个 HTTP 请求传递给用户。使用新文件
您将像调用普通 CSS 文件一样调用该文件。
结论
使用这种方法,您可以做一些很棒的事情。
@import
语句...这本身就很棒。您也可以对 JavaScript 使用相同的方法,尽管上面的
compress
函数严格用于 CSS,所以我将省略它。将此技术与缓存控制技术结合使用,您就可以为自己构建一个很棒的 CSS/JS 处理程序: 如何强制浏览器重新加载缓存的 CSS/JS 文件?
I find the best way to deal with minifying and compressing stylesheets is to do it yourself. Check out this code:
There's a lot of things going on here, so let me explain.
Headers
Last-Modified
andExpires
header for cache control (Setting at slightly over a week, you can change this).Minify and GZIP
ob_start
, we tell it to GZIP this file as well as run a custom functioncompress
.compress
removes all CSS comments and white space when sending to to the browser. This means you can keep all your comments and spacing the way you like it in your source files for easier editing, but only send the bare minimum to the browser.Style
require
to import the stylesheets. Do this for as many stylesheets as you'd like; They'll all get delivered in a single HTTP request to the user.Using Your New File
You'll call on the file just as you would a normal CSS file.
Conclusion
Using this method, you're doing several awesome things.
@import
statements...which in itself is awesome.You can use this same method for JavaScript as well, though the
compress
function above is strictly for CSS so I would omit it.Use this technique in combination with this cache control technique, and you've built yourself an awesome CSS/JS handler: How to force browser to reload cached CSS/JS files?