2

I have hosted one of my website on godaddy.com. How do I enable compression to reduce the file transfer size? I need to compress the .css and .js files. I tried the following but it doesn't work :

SetOutputFilter DEFLATE Header append Vary User-Agent 
  • The Header directive should be on a line by itself, but are you really varying the header based on the User-Agent? – MrWhite Feb 14 '15 at 13:14
1

By compressing .js and .css, do you mean to minfy them or gzip compression. If you need to minify, just use css minfier or JS minifier.

And if you are asking about gzip compression, it is also too easier. Just create an .htaccess file in your ftp root (only if it doesn't exist), and paste the below code to it,

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
| improve this answer | |
1

Considering you have go-daddy, you might not have decent apache access. Use PHP and use the following template.

<?php
$compression_level=2; //any number from 1 to 9 depending on how much compression
ob_start();
// INSERT YOUR OUTPUT CODE HERE
$data=ob_get_contents();
ob_end_clean();
if (strpos(strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') !== false){
$data=gzencode($data,$compression_level);
header("content-encoding: gzip",true);
}
echo $data;
?>

What will happen here is a buffer is created which holds all output. After that, the buffer is stored as a variable. Then it checks to see if the client can support gzip decompression and if it does, then compress the code and result is in compressed format and send the gzip header along with the compressed data back to the client.

This code also returns normal uncompressed output for those with browsers that can't support gzip encoding.

Once that is done, you can either make reference to the PHP file directly in the HTML code, like this for example:

<link rel="stylesheet" href="http://www.whatever.com/css/css.php">

Or if you want to make the filename look pretty, look up the RewriteRule command for mod_rewrite. There's lots of examples on the net for that.

| improve this answer | |
1

You could try what Godaddy recommonds on their site: Enabling mod_Deflate with Your Hosting Account. Add the following to your .htaccess file

AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
| improve this answer | |
  • I have tried this on my localhost,but it gives a 404 error on the files in the directory. – Suhail Gupta Feb 14 '15 at 5:25
  • 1
    @SuhailGupta By itself that should not result in a 404. Can you give an example of the request? Have you confirmed that it's not already compressed by checking the response headers? Have you confirmed your hosting type, as mentioned on the linked document? – MrWhite Feb 14 '15 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.