How do I enable compression to reduce the file transfer size on GoDaddy?

How do I enable compression to reduce the file transfer size on GoDaddy? - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, css, javascript, godaddy, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


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 

Solution :

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>


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.



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

Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect