Mamp - 让Gzip工作

时间:2017-07-05 15:31:43

标签: php css wordpress .htaccess

如何让gzip在MAMP中设置Wordpress网站?

我已经gulp创建了文件/dist/css/main.css.gz并将其添加到我的wordpress网站的标题PHP文件中。

然后我失去了所有的造型。我假设这是因为我还没有在.htaccess中启用它?所以我找到了以下代码添加到我的.htaccess

<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml

# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>

它仍然不起作用。

1 个答案:

答案 0 :(得分:2)

我不确定您为什么要将文件存储为gzip存档,但这不是这样做的方法。 AddOutputFilterByType DEFLATE text/css会在发送文件之前压缩文件。要解压缩现有文件,这样的东西将起作用(用2.2测试):

<Directory />
        AddOutputFilter INFLATE gz
        RemoveType .gz
        AddType text/css .css.gz
</Directory>

测试输出:

$ echo "body {color:black; background:white;}" > foo.css
$ gzip foo.css
$ curl http://localhost/foo.css.gz
body {color:black; background:white;}
$ curl -I http://localhost/foo.css.gz
HTTP/1.1 200 OK
Date: Fri, 07 Jul 2017 23:01:22 GMT
Server: Apache/2.2.15 (Scientific Linux)
Last-Modified: Fri, 07 Jul 2017 22:59:22 GMT
Content-Length: 38
Content-Type: text/css
$

当然,这会干扰您提供实际gzip文件的能力,并且您需要为每个压缩文件手动指定MIME类型。

此外,如果您正在控制服务器,则不应使用.htaccess个文件。它们会变慢并带来安全风险。把它放在服务器配置中!