IIS 7.5 gzip compression at shared hosting

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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>

Method 2

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

Method 3

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x