How to make gzip work on 1and1 shared host WordPress site

前端 未结 6 2025
温柔的废话
温柔的废话 2021-01-05 02:15

YSlow is telling me that my css should be compressed, but after several hours of tinkering, I cannot for the life of me get gzip to work for my website. At this point, I\'m

相关标签:
6条回答
  • 2021-01-05 02:49

    Enable gzip compression
    The gzip compression can be activated in php.ini with the following code:

    zlib.output_compression = On
    zlib.output_compression_level = 9
    allow_url_fopen = On
    
    0 讨论(0)
  • 2021-01-05 02:49

    You can enable compression by adding this code into your .htaccess file :

    <IfModule mod_rewrite.c>
    
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml image/svg+xml image/x-icon text/css application/x-javascript application/javascript application/x-httpd-php application/x-httpd-fastphp application/x-httpd-eruby 
    
    </IfModule>
    
    0 讨论(0)
  • 2021-01-05 02:53

    Works for me,

    First you have to copy the php.ini in all directory. (1and1 provided a script to facilitate this manipulation in their faq) with this content :

    zlib.output_compression =1
    zlib.output_compression_level =9
    

    Then add this in the htaccess :

    <IfModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_item_exclude file \.(gz|zip|xsl)$
    mod_gzip_item_include mime ^text/html$
    mod_gzip_item_include mime ^text/plain$
    mod_gzip_item_include mime ^image/x-icon$
    mod_gzip_item_include mime ^httpd/unix-directory$
    mod_gzip_item_include mime ^text/javascript$
    mod_gzip_item_include mime ^application/javascript$
    mod_gzip_item_include mime ^application/x-javascript$
    mod_gzip_item_include mime ^text/x-js$
    mod_gzip_item_include mime ^text/ecmascript$
    mod_gzip_item_include mime ^application/ecmascript$
    mod_gzip_item_include mime ^text/vbscript$
    mod_gzip_item_include mime ^text/fluffscript$
    mod_gzip_item_include mime ^text/css$
    </IfModule>
    
    0 讨论(0)
  • 2021-01-05 02:58

    It looks to me like you have exhausted your option. Looking at the above it seems like the host indeed doesn't have mod_deflate or mod_gzip. So I guess you are just out of luck.

    The PHP solution is indeed only for the HTML. So just stick with that one. HTML is also the best place to add compression, as, most of the time, the CSS and JS are only downloaded on the first page.

    You could redirect the request to CSS and JS though a PHP script, and use the PHP to compress. But I would not go there, as you would also have to implement 304 Not modified and set the appropriate expires headers.

    0 讨论(0)
  • 2021-01-05 03:10

    So, after a while I figured out how to compress html, css and js files having a 1&1 Webhosting package. Deflate is not supported!

    For the dynamic content you add php.ini to your root directory of your website. Content of php.ini:

    zlib.output_compression =1
    zlib.output_compression_level =9
    

    Of course you can also choose another compression level, 9 is the highest (and causing the highest server load). That will compress your the dynamically generated html file.

    To compress static files (css, js and images...) you need to modify the .htaccess file. For that append

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteOptions Inherit
    ReWriteCond %{HTTP:accept-encoding} (gzip.*) 
    ReWriteCond %{REQUEST_FILENAME} !.+\.gz$ 
    RewriteCond %{REQUEST_FILENAME}.gz -f 
    RewriteRule (.+) $1.gz [QSA,L] 
    </IfModule>
    

    to your .htaccess file (you find that file in the root directory of your website - else create it). But the compression is not done automatically. So you have to compress the files on your own! Use e.g. 7-zip and compress the js and css files with .gz -> the result should be e.g. stylesheet.css.gz. Then upload the file to the same directory as the file you just compressed.

    Now it should work!

    PS: compression is not always useful especially when the file is very small. So check the differences before and after compression.

    0 讨论(0)
  • 2021-01-05 03:10

    I know this question is a bit old now, but I've found a solution that works for me.

    Add a file called "php.ini" to the root folder containing the following;

    zlib.output_compression = On
    zlib.output_compression_level = 9
    

    Then (and this is the bit you might not expect) add the following to your .htaccess file;

    AddType x-mapp-php6 .html .htm .php
    

    Yes, that's right. I've put php6 in that. Apparently that will run the latest stable version of PHP (currently 5.4) which will allow gzip compression. This will also run .html and .htm files through the PHP parser, which means that they can be compressed (files not run through the PHP parser will not be compressed). Feel free to add any other extensions that you want to be run through PHP (.xml for example).

    By the way, if you do run .xml files through PHP, remember to set the header declaring it as an xml file, otherwise it won't work properly.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题