Jump to content
xisto Community
FirefoxRocks

Cpanel Optimize Website

Recommended Posts

I was told there is an option under Software/Services for "optimize website", where I can specify MIME types of files to use gzip compression on, but if the option is not visible then it is disabled by my hosting provider.What is the best way to do gzip compression without PHP? I have tried a few .htaccess lines but none of them worked.

Share this post


Link to post
Share on other sites

Of course it's best to do it with PHP somewhere in your script, to control it much easier, but sometimes some websites are just plain html files..

For PHP files, you can turn on these features through .htaccess by using php_value with attributes;

But for plain HTML files, I just found this code to use in .htacces, so maybe it will work for you? as I currently have no way to test 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>

Source: http://forums.xisto.com/no_longer_exists/

Share this post


Link to post
Share on other sites

That's the exact code I have right now, as I have also landed on that website as well, but I've checked a few sites and Firebug and it seems that the page still isn't gzipped.

Share this post


Link to post
Share on other sites

Well, I just tried it on localhost and it works, but I needed to turn those apache modules on through httpd.conf as by default they were not included..

also, I tried another way by using mod_deflate, it seems to work too:

# compress text, html, javascript, css, xml:#AddOutputFilterByType DEFLATE text/plain#AddOutputFilterByType DEFLATE text/html#AddOutputFilterByType DEFLATE text/xml#AddOutputFilterByType DEFLATE text/css#AddOutputFilterByType DEFLATE application/xml#AddOutputFilterByType DEFLATE application/xhtml+xml#AddOutputFilterByType DEFLATE application/rss+xml#AddOutputFilterByType DEFLATE application/javascript#AddOutputFilterByType DEFLATE application/x-javascript

But when I tried both those methods online on Xisto server, it did not work, so I made a conclusion, that apache doesn't have those modules turned on on the server? mod_deflate and mod_gzip for some kind of reason..

But I surely know it works with PHP on the server using ob_gzhandler, also you can parse css or js files using PHP as I do with gzip turned on. :P

For example, you can make a directory /css/ in that directory put file index.php and parse the css file.. in the HTML I just put a link to the css output by doing /css/?style.css and I send style.css name to PHP and do some checks and parse the right response headers and do the gzip stuff and output it :)

Of coruse, it would be much better to do it with the htaccess method. :D

Share this post


Link to post
Share on other sites

I was told there is an option under Software/Services for "optimize website", where I can specify MIME types of files to use gzip compression on, but if the option is not visible then it is disabled by my hosting provider.

It is not in Software/Services, it is in "Advanced" , you find "Mime types" :

MIME types tell browsers how to handle specific extensions. For example, the text/html MIME type equates to .htm, .html, and .shtml extensions on most servers, and this tells your browser to interpret all files with those extensions as HTML files. You can alter or add new MIME types specifically for your site (note that you can not alter the system defined MIME type values). MIME types are often used to handle new technologies as they appear. When WAP technology first appeared no one had these extensions set up on their server. With MIME types, however, you could have set it up yourself and begun serving WAP pages immediately.

And, in the standard settings, you see :

application/x-gtar gtar application/x-gzip .gz .tgz

Seems to be exactly what you were talking about, simply in the wrong section.

Share this post


Link to post
Share on other sites

I checked the MIME types thing already, but the extension for a gzipped HTML file is still .html

I know that ob_gzhandler works fine in PHP, and I'm already using a PHP file to compress, strip newlines and tabs from my CSS, but there is a problem on the PHP file. Here is the code at the beginning of the page:

<?php	error_reporting(E_ALL);	(float)$t = microtime(true);	header('Content-Type: text/html; charset=utf-8');	header('Content-Language: en-CA');		function callback($buffer) {		return str_replace("\n", '', str_replace("\r", '', str_replace("\t", '', $buffer)));	}		ob_start('callback');		require 'data_class.php';	$data = new DbMan ('localhost', DB_USER, DB_PASSWORD, DB_NAME, 3306);	$data->getFbidData();?>
Since I use ob_start('callback'), I can't use ob_start('ob_gzhandler');, despite the documentation about having multiple callback functions allowed ... unless someone can explain how to do it.

Share this post


Link to post
Share on other sites

Oh, just start several output buffers, I do it all the time, I personally have a template system working with output buffering, it's a callback function, and an ob_gzhandler, so this is the way I do it, I made the code to look much simpler.

Besides, you also need another ob_start(); without any callback, to return the right content length of the page, due to gzip one is different.

so this is how you start using ob_start somewhere in the constructor/start of your script:

<?phpob_start();if ( !ob_start("ob_gzhandler") ){  ob_start();}ob_start("ob_templater");?>

And somewhere in the destructor/end of the script, where you're sending you page/data to the user, use this method:

<?phpob_end_flush();ob_end_flush();header("Content-Length: ". ob_get_length() );header("Content-Type: text/html; charset=UTF-8");ob_end_flush();?>

When you're only using ob_start("ob_gzhandler"); then you send the page having 4000 bytes, but the header is sent from apache having 10000 bytes, so the browser might think something is bad, so I suggest/recommend to use the method I'm using and everything will be alright..

Also, when starting output buffering, it's recommended to check different things, I don't do it on my CMS, due to I know how my server works, so I always save several checks, but you can always do this:

// Start output bufferingob_start();// Start GZipped output bufferingif (DB_SETTING_IS_SET_TO_USE_GZIP && !headers_sent() && ( ob_get_length() == 0 ) && extension_loaded('zlib') && ( ini_get('zlib.output_compression') != 1 || ini_get('output_handler') != 'ob_gzhandler') ) {  # We need to send the right Content-Length header!  ob_start("ob_gzhandler");  // Define that we're using gZIP  define('GZIP_IS_ON', 1);}

and to end this script just use this:

// Turn off GZip output bufferingif (defined('GZIP_IS_ON') ) ob_end_flush();// Send the right header for length of the output bufferheader("Content-Length: " . ob_get_length() );// Send the output buffer and turn off output bufferingob_end_flush();

So this is how I'm using it.. As much as I tested, it's as fast as only using one output buffering, you can use even 5 ob_start() if you're needing it, but usually 3 is enough for everything, one for gzip, other for content length and the third one for your custom callback function. You can't use the custom callback for content length, as usually it's used to change content, like replacing {title} to title value from DB or like you do to remove whitespaces, so the content length is changing. :)
Edited by Quatrux (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.