enable gzip compression on apache server

How to enable GZip compression on Apache Server?

How to enable GZip compression on Apache Server

How to enable GZip compression on Apache Server

GZip compression is used to increase speed of web page in your site. Your browser send request to server and get all the images, javascript and css files from server. When your web page have lots of images, javascript and css files than its take time to load. GZip compression gives boost to your website speed and save bandwidth as well.
In a very simple language, GZip compress all the data and send to browser. All the latest browser understands and accept compressed files.
There are two major ways to enable the GZip Compression on the Apache Server web-sites.

1) Using httpd

You need to install mod_deflate module, if it is not installed than you can download it from Apache httpd Modules and add it to httpd modules directory on your server.

Once you add mod_deflate module in your httpd modules directory, you completed half work. Now open your “httpd.conf” file and search for “mod_deflate”. You will find the following line

LoadModule deflate_module modules/mod_deflate.so

If it is commented than uncomment it, so it will LoadModule directive for mod_deflate.

Now we need to create a Deflate Rule Set which tells your apache server that compress which types of file.

Create “httpd-deflate.conf” file and copy following code in that file

#Set GZip for all output
SetOutputFilter DEFLATE
#Not Include the below file types
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|iso|tar|bz2|sit|rar|png|jpg|gif|jpeg|flv|swf|mp3)$ no-gzip 
#Use compression Level
DeflateCompressionLevel 9
#You can Handle browser specific compression using following rules
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0

Now, open “httpd.conf” file and add following code

# Deflate configuration
include <path of file>/httpd-deflate.conf

Now restart your Apache Server and its working GZip Compression. Your website will load 60% faster than before.

2) Using .htaccess file

You can add following code in your htaccess file

<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.*

If above code is not working than you can try another code which is as below

<ifModule mod_gzip.c>
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

how to enable mod_rewrite on iis server for php pages

Recently, I am working with one project which is hosted on the windows IIS server. I used .htaccess for URL rewrite rules for SEO(Search Engine Optimization) purpose.

I see that its not working on the IIS while it is working perfect on my local apache server. I see that may be URL Rewrite module may not be installed on the IIS server.

After looking on the IIS configuration, I knew that URL Rewrite module is installed but its not working similar to apache mod_rewrite. I need to enable mod_rewrite on IIS server same way its works in Apache server. After spending lots of time, I found the solution for this.

I will show you simple way using that you can get similar functionality like Apache on IIS Server.

You need to create web.config file in your root directory using below code.

Your web.config file code to test:

	<?xml version="1.0" encoding="UTF-8"?>
					<clear />
					<rule name="Redirect to google.com" stopProcessing="true">
						<match url="^google$" />
						<action type="Redirect" url="http://www.google.com/" appendQueryString="false" />

Once you did this than its time to test that its working or not?
To test try to open http://www.domain.com/google in browser where domain.com is your website’s domain name. If you are redirected to Google.com website means your URL rewrite module installed and working same like apache mod_rewrite.

This is just to check that Microsoft URL Rewrite module installed on your IIS server or not, if its installed than change your web.config file with below code:

Your final web.config file code:

	<?xml version="1.0" encoding="UTF-8"?>
			<rule name="Security Rule" stopProcessing="true">
			  <match url="^(.*)$" ignoreCase="false" />
			  <conditions logicalGrouping="MatchAny">
				<add input="{QUERY_STRING}" pattern="mosConfig_[a-zA-Z_]{1,21}(=|\%3D)" ignoreCase="false" />
				<add input="{QUERY_STRING}" pattern="base64_encode.*\(.*\)" ignoreCase="false" />
				<add input="{QUERY_STRING}" pattern="(\&lt;|%3C).*script.*(\>|%3E)" />
				<add input="{QUERY_STRING}" pattern="GLOBALS(=|\[|\%[0-9A-Z]{0,2})" ignoreCase="false" />
				<add input="{QUERY_STRING}" pattern="_REQUEST(=|\[|\%[0-9A-Z]{0,2})" ignoreCase="false" />
			  <action type="CustomResponse" url="index.php" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
			<rule name="SEO Rule">
			  <match url="(.*)" ignoreCase="false" />
			  <conditions logicalGrouping="MatchAll">
				<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
				<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" pattern="" ignoreCase="false" />
				<add input="{URL}" negate="true" pattern="^/index.php" ignoreCase="false" />
				<add input="{URL}" pattern="(/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$" />
			  <action type="Rewrite" url="index.php" />

if you get Cakephp : 404 – File or directory not found error on the windows server than please check Microsoft URL Rewrite module installed on your IIS server or not? If not than you need to enable mod_rewrite on iis server to run your cakephp project.

301 redirect in cakephp (moved permanently in cakephp)

301 redirect in cakephp (moved permanently in cakephp)

How to permanently 301 redirect in cakephp?
Best way for 301 redirect (permanently redirect).
301 redirect without .htaccess file.

Recently, i am working with cakephp project, and my client need to redirect all the old links to new links. As its a ecommerce website, it have the category and products but the old php developer made single page with two parameter in the query string, where one is cid which indicate the category and another one is pid which indicate the product. all the links are not seo friendly and its look like as below

N Numbers of links in the project but i show 3 example where we need to do 301 redirect from old links to new links.

 // Old Links
  productcategory.php?cid=1&amp;pid=3      // Category and Product Page 
  productcategory.php?cid=5&amp;pid=31     // Category and Product Page
  productcategory.php?cid=22           // Only Category Page
 // New Links 
  product/1/electronics/3/iphone      // Category and Product Page 
  product/5/games/31/play-station-4    // Category and Product Page
  category/22/beauty/                  // Only Category Page

301 redirecting is not easy and we need to workout with the rewrite rules using .htaccess file but i did it with easily in CakePHP. In cakephp 301 redirect like this is possible without .htaccess file. you need to create the productcategory.php in your webroot folder and place below code in that.

	$siteurl = "http://www.freelancephpdevelopment.com/";
	$cid = isset($_GET['cid']) ? $_GET['cid'] : '';
	$pid = isset($_GET['pid']) ? $_GET['pid'] : '';		
		case '1':
				$CategoryName = "electronics";
		case '5':
				$CategoryName = "games";
		case '22':
				$CategoryName = "beauty";
		case '3':
				$ProductName = "iphone";
		case '31':
				$ProductName = "play-station-4";
	if($cid != '' && $pid != '')
		header("HTTP/1.1 301 Moved Permanently"); 
		header("location: ".$siteurl."product/".$cid."/".$CategoryName."/".$pid."/".$ProductName);
	else if($cid != ''){
		header("HTTP/1.1 301 Moved Permanently"); 
		header("location: ".$siteurl."category/".$cid."/".$CategoryName);
	 else {
		header("HTTP/1.1 301 Moved Permanently"); 
		header("location: ".$siteurl);

After reading my article, I hope that you can resolve your 301 redirect cakephp issue. still you are facing any issue then please let me know and i will be happy to help you.

PHP Fatal error: Allowed memory size of 8388608 bytes exhausted….(Resolved)

When you are working with the file upload,read file or write file then you get the issue regarding the memory size, its give you fetal error in php which is starting with below text.

PHP Fatal error: Allowed memory size of

This is not the programming error but you face this error because of server configuration, by default server allowed less memory because of better performance of server and server do not hang over. you can set the memory size using 3 ways. you can use any one from below 3 points.

1) ini_set(“memory_limit”,”64M”) : Here 64M indicate that you want to allowed up to 64MB. you can set it as per your requirement. once you set this then please put this code in the starting of your php file.

2) memory_limit = 64M : Here 64M indicate that you want to allowed up to 64MB. you can set it as per your requirement. Please update this in the php.ini file.

3) php_value memory_limit 64M : Here 64M indicate that you want to allowed up to 64MB. you can set it as per your requirement. Please place this code in your .htaccess file.

So, you can use it as per your convenience, if you want it for all files then you can change in .htaccess file, if you want on the server then you can change it in the php.ini file and if you need this configuration for particular file then you can use first option and place in your php file.

how to stop directory listing in apache web server

when you are working with the Apache server,  might be you place upload folder to upload the images and files in that, also some time you did not create index.html, index.htm or index.php file in the folder. some time you use any directory to transfer the files. now if above is the case then when you try using the link below then you see the result which is show below it.


Result show like list of all the directory inside that directory, in the given example importantfiles is the folder and there is 3 other folder named 4.0_r3, current and project in that folder.

As a security reason this is not good and you need to prevent it. i can help you how to prevent directory listing on apache server.

You need to create .htaccess file and need to write below code in that htaccess file. once its done then upload that file in the root directory or the directory in which you want to stop directory listing. if you put it in root directory then its stop directory listing for root directory and all the sub-directory inside that directory.

Options -Indexes

When you did this then if there is no index file then its give you result like below:


You don’t have permission to access /uploads/ on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/ Server at ilibya.com Port 80

This is the 403 error and if you get this error then you want to redirect on any custom created page or template then use below line in the .htaccess file.

ErrorDocument 403 /index.php

You can set your own file name where i put the index.php, using above rule you can redirect on custom template.

How to redirect on index page or the home page when 403 error or 404  error found on the Apache server.

if you get error 403 or error 404 and you want to redirect user on home page or any particular custom page then please write below code in your .htaccess file.

ErrorDocument 403 http://www.yourdomain.com
ErrorDocument 404 http://www.yourdomain.com

Now you can stop the directory listing in your web-site.

set session to access on main domain and sub-domain, session access to all sub-domain

if you are creating sub-domain for modules and want to use the same session across all subdomains then you need to setup the configuration.

if you want to set session to access on main domain and sub-domain, that means session access to all sub-domain, i mean sessions across subdomains.

There is three way to set the configuration for this.

1) open php.ini file and search session.cookie_domain, set the main domain there without www, for example your main domain is www.example.com then you need to set session.cookie_domain = .example.com.
2) set using .htaccess file. set php_value session.cookie_domain .example.com in your .htaccess file.
3) using php_ini function you can set. ini_set(‘session.cookie_domain’,’.example.com’ ).

I think there is not any easy way then above three, enjoy the multiple domains……

not apply .htaccess on js files, not apply .htaccess on css files, not apply .htaccess on images

When you make the .htaccess file and you do not want to apply rules wrote in .htaccess file then you can do it using the condition in the .htaccess file, RewriteCond is used to place the condition in the rules.

Syntax for RewriteCond


Here, RewriteCond  is the keyword and the STRING CONDITION is your condition.


if you do not want to apply rules on js files included in the page then you can write the condition as below

RewriteCond %{REQUEST_URI} !\.(js)$

if you do not want to apply rules on images included in the page then you can write the condition as below

RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$

if you do not want to apply rules on css files included in the page then you can write the condition as below

RewriteCond %{REQUEST_URI} !\.(css)$

you can also apply more then one condition on the rules, below is the example if you do not want to apply rules on js,images and css files then use below code

RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$
RewriteCond %{REQUEST_URI} !\.(css)$
RewriteCond %{REQUEST_URI} !\.(js)$
Your Regular Rules

in the above example all three condition will apply when the rules below that condition are applicable.

remove index.php from url php

using .htaccess file, you can easily remove the index.php from the url, its just two line code you need to set in the .htaccess file which is as below

RewriteCond $1 !^(images|system|themes|favicon\.ico|robots\.txt|index\.php) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]

when you use above code make sure that Rewrite Engine is On, if you are not aware on this then look at the top of the .htaccess file, and search below code. if below code is not in your .htaccess file then please put it at the beginning of the .htaccess file.

RewriteEngine On

Now, you know how easy to remove the index.php form the url. enjoy…..

Block a Specific IP Address using .htaccess file

How to Block a Specific IP Address using .htaccess file

if you want to block a specific ip address for security reason or any other reason then you can block it using the .htaccess file, its very simple to write rules in htaccess file. below is the syntax and example to block the IP Address.

Syntax :

RewriteCond %{REMOTE_ADDR} ^(A\.B\.C\.D)$
RewriteRule ^/* http://www.yourdomain.com/example.html [L]

Example :

RewriteCond %{REMOTE_ADDR} ^(79\.65\.128\.89)$
RewriteRule ^/* http://www.yourdomain.com/example.html [L]

Please keep in mind that \ is required after all the number of IP Address, if you do not put \ then its not working.

Redirect Multiple Domains to a Single Domain


Some time company or organization buy many domain like .com,.net,.info,.in,.co.in,co.uk etc… If you have multiple domains then you need to setup site for all domain, here is the solution using that there is only one site set-up and pointing other site on main site, using below code in the .htaccess file you can redirect as many domain to a single domain.

RewriteCond %{HTTP_HOST} ^www.yourdomain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^yourdomain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^yourdomain.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]

Hope this is more useful to you if you using multiple domain.