apache 通过 .htaccess 进行 Javascript 压缩?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/373725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:03:13  来源:igfitidea点击:

Javascript compression via .htaccess?

javascriptapache.htaccess

提问by

I'm using Apache.

我正在使用阿帕奇。

I'm auto gzipping my HTML & CSS files on the fly using the following directive in my .htaccess file.

我正在使用 .htaccess 文件中的以下指令自动压缩我的 HTML 和 CSS 文件。

# Enable ETag
FileETag MTime Size


# Set expiration header
ExpiresActive on
ExpiresDefault "access plus 1 year"
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType text/css A2592000
ExpiresByType text/javascript A2592000
ExpiresByType text/js A2592000


# Compress some text file types
AddOutputFilterByType DEFLATE text/html text/css text/xml application/x-javascript text/javascript text/js


# Deactivate compression for buggy browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

However, when I use the Firebug plugin for Firefox, I notice that my javascript files are NOT being gzipped (only my HTML and CSS files are).

但是,当我使用 Firefox 的 Firebug 插件时,我注意到我的 javascript 文件没有被 gzip(只有我的 HTML 和 CSS 文件)。

Any ideas why my JavaScript files are not being gzipped on the fly by Apache?

任何想法为什么我的 JavaScript 文件没有被 Apache 即时压缩?

回答by

I figured it out, looks like I need it to be:

我想通了,看起来我需要它:

AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/x-javascript text/javascript application/javascript

To get javascript to compress/gzip

让javascript压缩/gzip

回答by Jim Puls

Have you declared the text/javascript MIME type in Apache? If your server configuration points to, say, a mime.typesfile with one of these:

您是否在 Apache 中声明了 text/javascript MIME 类型?如果您的服务器配置指向,例如,mime.types具有以下其中之一的文件:

TypesConfig /private/etc/apache2/mime.types

then said file should include this line:

然后说文件应该包括这一行:

application/javascript              js

Otherwise, you should see one of these somewhere:

否则,您应该在某处看到以下其中之一:

AddType application/javascript .js

At least, this is my first guess, if HTML and CSS files are being compressed and JavaScript files aren't.

至少,这是我的第一个猜测,如果 HTML 和 CSS 文件被压缩而 JavaScript 文件没有被压缩。

回答by SDMurphy

Try putting your declarations on separate lines. Here is the relevant portion of one of my .htaccess files which is functioning as desired. If you keep your scripts in a separate directory (such as /js) then this file should be in that directory as well.

尝试将您的声明放在不同的行上。这是我的 .htaccess 文件之一的相关部分,它按预期运行。如果您将脚本保存在单独的目录(例如 /js)中,则该文件也应位于该目录中。

AddOutputFilterByType DEFLATE text/plain

AddOutputFilterByType DEFLATE text/html

AddOutputFilterByType DEFLATE text/xml

AddOutputFilterByType DEFLATE text/css

AddOutputFilterByType DEFLATE text/javascript

AddOutputFilterByType DEFLATE text/js

AddOutputFilterByType DEFLATE application/xml

AddOutputFilterByType DEFLATE application/xhtml+xml

AddOutputFilterByType DEFLATE application/rss+xml

AddOutputFilterByType DEFLATE application/javascript

AddOutputFilterByType DEFLATE application/x-javascript