How can I make my .htaccess file allow Safari & ot

2019-03-03 16:36发布

问题:

I'm using an .htaccess file to allow my pages to call gzipped JavaScript files.

It works fine in ie8 & ff3, but the script is not loaded in Safari (or Chrome).

Here is the .htaccess file I'm using:

<files *.js.gz>
ForceType text/javascript
Header set Content-Encoding: gzip
</files>

Then, for example, I can call JS files from my HTML pages:

<script src="foo.js.gz"></script>

How can I modify the above .htaccess code to get it working for Safari (and still work for other browsers)?

回答1:

I assume you're trying to use Gzip compression for faster downloads of assets and text content. It's a great technique for speeding up your site.

Safari, Chrome, and IE6 all have problems with Gzipped downloads. Also, Apache will do the gzip compression for you, there is no need to manually gzip files. Try this snippet:

# This uses mod_deflate, which is pretty standard on Apache 2.  Loading
# mod_deflate looks like this:
#
#   LoadModule deflate_module modules/mod_deflate.so
#
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \\bMSIE !no-gzip !gzip-only-text/html

That should safely gzip all text before it's sent out from Apache, and only for browsers that support it. You can link to JavaScript and other assets normally, just myscript.js, with no gz extension or compression on disk.