2

I have a web-site, which uses ASP.NET and is hosted at IIS 7.5 shared hosting, so I have no direct access to IIS settings. Now I want to enable gzip compression of my pages and css/js files using IIS capabilities, but none of recipes found at the Internet worked for me. For example, when I add what is written here to my Web.config, nothing changes: no errors, no compression.

Is this possible? If not, what's the best alternative?

aplavin
  • 2,199
  • 5
  • 32
  • 53

3 Answers3

5

I encountered the same problem, and so used .net's compression invoked using global.asax along-with renaming static css with .aspx extension and using url rewrite through Web.config Atop the .css.aspx I simply added

<%@ Page ContentType="text/css" %>

In rules of rewrite of system.webserver of configuration of Web.config:

<rule name="san aspx">
    <match url=".*[^/]$" />
        <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
    <action type="Rewrite" url="{R:0}.aspx" />
</rule>
<rule name="revert aspx">
    <match url="(.*)\.aspx$"/>
        <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
    <action type="Rewrite" url="{R:1}" />
</rule>

In Application_PreRequestHandlerExecute of global.asax

HttpApplication app = sender as HttpApplication;
string acceptEncoding = app.Request.Headers["Accept-Encoding"];
Stream prevUncompressedStream = app.Response.Filter;
if (acceptEncoding != null && acceptEncoding.Length > 0) {
    acceptEncoding = acceptEncoding.ToLower();
    if (acceptEncoding.Contains("gzip")){
        app.Response.Filter = new GZipStream(prevUncompressedStream, CompressionMode.Compress);
        app.Response.AppendHeader("Content-Encoding", "gzip");
    }
}    
Response.Cache.VaryByHeaders["Accept-Encoding"] = true;

It appears intimidating, but done with building proper understanding, it works like charm and is worth the savings on SEO related with PageSpeed as well as shared hosting. I did try with my hosting provider's support, but they just marketed more exotic hosting packages instead. For compulsorily compressed content like .svgz file, I've used separate folder and httpProtocol of system.webserver of that folder's web.config I wrote:

<customHeaders>
    <add name="Content-Encoding" value="gzip" />
</customHeaders>
Chawathe Vipul S
  • 1,636
  • 15
  • 28
3

Try the configuration sample at the bottom of this article:

http://www.iis.net/ConfigReference/system.webServer/urlCompression

<configuration>
   <system.webServer>
      <urlCompression doStaticCompression="true" doDynamicCompression="false" />
   </system.webServer>
</configuration>

In this question it says that:

"Yes you can enable compression with the web.config, as the article below shows- but it can depend on the permissions on the server allows sites."

See if this helps you:

IIS 7.5 ASP.NET-4 Gzip compression

Or this:

GZip Compression On IIS 7.5 is not working

Community
  • 1
  • 1
Răzvan Flavius Panda
  • 21,730
  • 17
  • 111
  • 169
  • It doesn't work either, so as I understand, server doesn't give me such permissions? I'm confused by the fact that it doesn't show me any errors. – aplavin Aug 24 '11 at 08:12
  • 4
    Well, i am thinking that maybe server is set not to allow gzip compression to save CPU/memory resources. You should check with your server provider if it is disabled on server and if it is anyway to enable it. – Răzvan Flavius Panda Aug 24 '11 at 08:31
0

I know this is super old but I found this link which actually worked for me. I have arvixe hosting and they dont allow compression either. I tried everything in web.config that didnt work but this in the global worked

http://forums.asp.net/t/1599397.aspx?Best+Way+to+Enable+HTTP+Compression

SeanRouz
  • 23
  • 7