ad

Compress CSS with PHP and Optimize Website Performance.

timthumb

Most of the web masters face the problem of high load time of the website. If your website takes time on loading on users browser, you need to think about it. Because most of the internet users do not want to wait extra for a website to load on browser. User use to switch to another website in this case. It can be a loss of your internet business.

Compress CSS with PHPMost common reasons which slow down the website performance are high size of CSS files and JavaScript files, images and other high size graphical content. But css and js will mostly be included in almost all the pages of any dynamic website, So it affect almost very page of your website.

Speeding up your website can be done by reducing number of css and js files, removing heavy plugins optimizing website images and reducing the size of your cs files. We can reduce the filesize of a CSS file by a very considerable ratio by removing unnecessary white spaces, comments, also making a bit modifications in the code.

CSS file compression is also a nice way to improve the website speed. By minimizing the external file sizes which also take time in loading. In general we use some external scripts to compress the CSS files. But in this tutorial i will show you how to compress Compress CSS with PHP.

1. You can compress your CSS files with PHP. But you need to save it as PHP file. Write this PHP code just before the style.php file

<?php
ob_start ("ob_gzhandler");
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " .
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>

body { color: red; }
H1{color:Blue;}

/* and other style rules */

Attach this file to your page as

<link rel='stylesheet' type='text/css' href='css/style.php' />

2. If you do not want to create stylesheet as a php file, you can follow the htaccess way. Create a PHP file gzip-css.php with following code.

<?php

ob_start ("ob_gzhandler");

header ("content-type: text/css; charset: UTF-8");

header ("cache-control: must-revalidate");

$offset = 60 * 60;

$expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";

header ($expire);
?>

Now add these htaccess rules in .htaccess file.

<FilesMatch "\.(css)">
ForceType application/x-httpd-php

php_value auto_prepend_file "/the/full/path/of/this/file/gzip-css.php"

</FilesMatch>

Whenever http request for .css file will come to the web server, it will be converted to application/x-httpd-php. Then gzip-css.php will be prepend to this request to compress the output of the css file.

Tags:
Nord VPN Sale

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Developed By Deepanker