Home  »  Optimization  »  Hacking WP Super Cache for Speed

by 11 comments

So I couldn't help it, I downloaded and installed WP Super Cache because it sounded like an ideal setup for me.. A plugin built to generate static files from php+mysql for Apache to serve the way its supposed to be.. My dream.

So I started digging into the code. Its a form of WP Cache-2, but fork is a generous word at this point. There is some future add-on capability, but the code that actually performs the caching is pretty much the same. Bloated, inefficient, and redundant coding. This plugin could do the same thing with 1 file, no config, no meta, serializing, etc.. But its still a lot better than anything I've created.

AskApache Crazy Cache - coming soon

I'm very interested in this technology and this is the closest to my ideal that a WordPress plugin has come so far, I might just start from scratch one of these days, once I figure out a few php bits I'm still learning about. If I do decide to make a caching plugin the right way, it will have similar tricks that I used in the Hack WP-Cache for maximum speed post.

My WP Super Cache .htaccess

The .htaccess generated by WP Super Cache is wack, I'm not even sure how its been working for everyone, I've hacked it up a bit and this is working great for me.. Will definately be updated as I do more debugging.

I used some of the same techniques found here, and here, and these SetEnvIf examples.

# BEGIN WordPress

SetEnvIfNoCase ^Accept-Encoding$ "(.*gzip.*)" HTTP_IF_GZ_MATCH=.html.gz

RewriteCond %{ENV:REDIRECT_STATUS} !=200
RewriteCond %{QUERY_STRING} !s
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+) HTTP/ [NC]
RewriteCond %{HTTP_COOKIE} !^.*(comment_author_|wordpress|wp-postpass_).*$
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/%1/index%{ENV:HTTP_IF_GZ_MATCH} -f
RewriteRule ^(.*)$ /wp-content/cache/supercache/$1/index%{ENV:HTTP_IF_GZ_MATCH} [L,NC]

# END WordPress

WP Super Cache .htaccess

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !.*s=.*
RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
RewriteCond %{HTTP_COOKIE} !^.*wordpress.*$
RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html.gz -f
RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html.gz [L]

RewriteCond %{QUERY_STRING} !.*s=.*
RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
RewriteCond %{HTTP_COOKIE} !^.*wordpress.*$
RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html -f
RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html [L]

# END WordPress

It's definately a complex kind of plugin in that there is a lot of directions and a lot going on, so I'm not saying anything bad about the developers, just hope to see some updates over the next few months so I don't have to mess with it.


January 30th, 2008

Comments Welcome

  • Stuart

    Hi, do you think you could put the date somewhere on the post. It's a little annoying landing here from google and not knowing if what I'm reading is fresh or outdated, especially when it says stuff like this article says.

    To find the date of this particular page I had to go to your homepage, look down the sidebar, click on the 'Cache' tag, and find the excerpt among the index listings. Not high usability by any means :(

  • stuart

    I agree with the other comment, I've just been looking for a date and I'm not sure if this article is still valid.
    offtopic: his name is stuart aswell, what are the odds?

  • AskApache

    @ Stuart

    Thank you for taking the time to let me know, I've added the date under the title great suggestion!

    This article of mine really isn't that good to begin with, I'm still waiting on more improvements before I switch from wp-cache.

  • Mike

    While I agree that the rules aren't entirely optimal, there's a problem with your ruleset.

    "RewriteCond %{HTTP:Accept-Encoding} gzip" in the original causes a Vary: Accept-Encoding header to be appended if the rule passes as true.

    Without that header, a front-end or intermediate cache could, for example, send cached gzipped content to a UA that doesn't support it.

  • AskApache

    I ended up just making my own caching plugin which is still in house.. Its pretty cool for pimping this server out. Right now I'm serving up both text/html application/xhtml+xml... Both of which are available in gzip as well and both of which validate as strict as it goes.

    I've been playing around with other forms of media that I can also incorporate.. I've already added custom rdf files for every page using the same caching system which I might release when I finish with this password protection upgrade.

    Its based on my lightning cache plugin.


    Here's some of the code I'm at today if it helps..

    RewriteCond %{REQUEST_METHOD} !^(GET|HEAD) [OR]
    RewriteCond %{QUERY_STRING} !^$ [OR]
    RewriteCond %{HTTP_COOKIE} ^.*(comment_author_|wordpress|wp-postpass_).*$ [NC]
    RewriteRule ^(.*)$ - [S=6]

    RewriteCond %{HTTP_USER_AGENT} .*W3C_Validator.* [NC]
    RewriteCond %{HTTP:Accept-Encoding} gzip [NC]
    RewriteCond %{DOCUMENT_ROOT}/fast-cache/$1/index.xhtml.gz -f
    RewriteRule ^(.*)$ /fast-cache/$1/index.xhtml.gz [L,S=5]

    RewriteCond %{HTTP:Accept} application/xhtml+xml [NC]
    RewriteCond %{HTTP:Accept-Encoding} gzip [NC]
    RewriteCond %{DOCUMENT_ROOT}/fast-cache/$1/index.xhtml.gz -f
    RewriteRule ^(.*)$ /fast-cache/$1/index.xhtml.gz [L,S=4]

    RewriteCond %{HTTP:Accept-Encoding} gzip [NC]
    RewriteCond %{DOCUMENT_ROOT}/fast-cache/$1/index.html.gz -f
    RewriteRule ^(.*)$ /fast-cache/$1/index.html.gz [L,S=3]

    RewriteCond %{HTTP_USER_AGENT} .*W3C_Validator.* [NC]
    RewriteCond %{DOCUMENT_ROOT}/fast-cache/$1/index.xhtml -f
    RewriteRule ^(.*)$ /fast-cache/$1/index.xhtml [L,S=2]

    RewriteCond %{HTTP:Accept} application/xhtml+xml [NC]
    RewriteCond %{DOCUMENT_ROOT}/fast-cache/$1/index.xhtml -f
    RewriteRule ^(.*)$ /fast-cache/$1/index.xhtml [L,S=1]

    RewriteCond %{DOCUMENT_ROOT}/fast-cache/$1/index.html -f
    RewriteRule ^(.*)$ /fast-cache/$1/index.html [L]

    FileETag None

    AddEncoding x-gzip .gz

    AddType 'application/xhtml+xml; charset=UTF-8' .xhtml
    AddType 'text/html; charset=UTF-8' .html

    Header set P3P "policyref="""
    Header set Content-Language "en-US"

    # BEGIN AskApache Crazy Cache
    FileETag All

    AddEncoding x-gzip .gz

    AddType 'application/xhtml+xml; charset=UTF-8' .xhtml
    AddType 'application/xhtml+xml; charset=UTF-8' .xhtml.gz

    AddType 'text/html; charset=UTF-8' .html
    AddType 'text/html; charset=UTF-8' .html.gz

    Header set P3P "policyref="""
    Header set X-Pingback ""
    Header set Content-Language "en-US"
    Header set Vary "Accept-Encoding,Accept"

    ExpiresActive On
    ExpiresDefault A3600

    # END AskApache Crazy Cache

  • Donace

    you got a due date on that inhouse plugin release? or will it remain in house?

  • Sunshine

    When using the code with AudioPlayer 2.0b plugin the audio doesn't play in Firefox and in IE it is compressing the audio and putting noise in it. What needs to be changed to work with audio player?

    Overall it works great and load the site extremely fast.

  • erick

    Data caching provides the most granular control of cached data. The data cache is a full-featured cache engine that enables you to store and retrieve data between multiple HTTP requests and multiple sessions within the same application. There are three different methods that you can use to add data or objects to the cache.

  • ticktock

    1 question - how does one ask apache to log performance metrics of an .htaccess file?

  • seriocomic


    I tried your version of the .htaccess rules for WP-Super-Cache, and it stopped sending gzip'd content. Putting the plugin's .htaccess rules fixed it. Any chance of an update/review since this post?

    Additionally, i love your articles - great resource of info - but it would help (as others have stated) if there was a date stamp on the entries and comments so I can determine if they are out of date or not.


  • Arturo

    Hi, don't know if is old news or not, for me W3 Total Cache is an alternative, personally i installed this plugin, i'm curious what you think about this Wordpress plugin.

Related Articles

My Online Tools
Popular Articles

Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman


It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | askapache

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service

↑ TOPMain