W3 Total Cache – Cookie Is Rejected

Recently, I switched over to W3 Total Cache. Reasons for the switch –

  • There have been a lot of great reviews and raves about W3 Total Cache.
  • W3 Total Cache offers a very comprehensive caching solution.
  • I was having caching issues on my multi-site setup.

In general, I am very happy with the results. There is a bit of a learning curve initially, but W3 Total Cache is well worth the effort. A BIG thanks to Frederick Townes for creating such an awesome plugin.

This article describes some common W3 Total Cache issues and how to fix them.

1. W3 Total Cache – Cookie Is Rejected

This is a common issue that arises whenever a user makes a comment on your blog. Making a comment causes a cookie to be set, and this disables caching for that user until the cookie is cleared or expired.

Here is an explanation by Frederick Townes.

In WordPress 3.0, the default comment cookie expiration time is 30,000,000 seconds which is about 8333 hours or 347 days. That is a long expiration time.

Unless the user manually clears his browser cookies, he will not get to enjoy the super caching capabilities on your site for about a whole year.

A simple way to fix this is to use the comment_cookie_lifetime filter which will only get applied for non-logged in users.

// Add filter to the init function of your theme functions.php file
add_filter('comment_cookie_lifetime', 'my_comment_cookie_lifetime');	
	
function my_comment_cookie_lifetime($lifetime) {
	return 10;
}

The code above shortens the comment cookie expiration time to 10 seconds. After 10 seconds, caching will be re-enabled for the user. I am just using 10 seconds as an example. You may set the expiration time to whatever works best for your environment.

You can test this by turning on Page Cache debugging in the main Performance >> General Settings screen.

2. W3 Total Cache – URL Redirect Issues

If you have any rewrite rules in your .htaccess file, for example to remap your domain from www.site.com to site.com or vice versa, then make sure to put those rewrite rules before the W3 Total Cache rules.

For example –

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

# BEGIN W3TC Page Cache
<IfModule mod_rewrite.c>
...

Here is a discussion of the redirect issue and the fix.

3. W3 Total Cache – Duration of Page Cache Files

If you want to change how long your pages are cached, it is not sufficient to just change the garbage collection interval in the Page Cache screen.

As stated in the notes section at the bottom of the Page Cache screen –

The TTL of page cache files is set via the “Expires header lifetime” field in the “HTML” section on Browser Cache Settings tab.

TTL = time to live.

4. W3 Total Cache – WordPress Multi-Site Settings

It is important to note that some W3 Cache settings affect the local .htaccess file (i.e., the .htaccess file on the main directory of your WordPress installation), while others affect the .htaccess file of your domain.

Many Browser Cache settings (e.g., those pertaining to header expiration) for example, change your domain .htaccess file. Page Cache settings on the other hand make changes to the .htaccess file that is local to your WordPress installation.

In WordPress multi-site (with sub-directories), several different blogs share the same local .htaccess file. As a result, certain W3 Cache Settings have to be identical for all of your blogs. In particular, I make the Page Cache and Browser Cache settings identical for all of my multi-site blogs. Otherwise, I get an error saying that Page Enhanced mode is disabled.

Here is a thread discussing this issue in detail.

Note – I also make header expiration settings in the Browser Cache screen identical for ALL blogs in my domain because that affects the main domain .htaccess file.

W3 Total Cache Is Awesome!

The purpose of this article is to summarize some of the things I learned while setting up W3 Total Cache.

Some time and effort is needed initially, but the rewards from the plugin are great. Make sure to get comfortable with the plugin on a test site first before installing it on your main site.

Related Articles

Comments

  1. Ben Zittlau says

    Thanks for the number 3 point. I was suspicious that the garbage collection interval wasn’t actually going to expire my pages based on the description, but I didn’t relate the comment at the bottom to that point.

  2. Stephen says

    super useful. I’m having consistent cookie rejected issues, it’s a pain. And while it says gzip is enables I don’t actually see any compression. Any ideas?

  3. says

    You have an extra ) in your code for the “cookie is rejected” issue.

    The add_filter command should be:

    add_filter(‘comment_cookie_lifetime’, ‘my_comment_cookie_lifetime’);

Trackbacks

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>