How To Remove Cookie In WordPress Plugin Wp Super Cache Http Response Header Vary

WP Super Cache is a very popular WordPress plugin. It can generate an Html version of all your WordPress articles. Then when the client browser requests your article, it will return the Html version instead of executing the dynamic web page. This can improve page response speed and make your website more efficient.

But when you look at your web page HTTP response header generated by WP Super cache, especially the Vary header’s value, you will find the Vary header value includes string ‘Cookie’ like this ( Vary: Accept-Encoding, Cookie ).

This will make the intermediary proxy server’s cache or your web browser’s cache inefficient. Because the cache will save cached web page by cookie value, so every time a request to the same web page from a different cookie user, the proxy server will get the web page from the original web server, not from the cached web page, this makes the cache not working at all. You can read the article How To Set HTTP Header Vary Value Correct To Make Cache Work Effective to learn more.

1. How To Find The PHP Source File That Add Vary Header.

  1. Download your WordPress website source code into your local computer.
  2. Install a file editor, like sublimetext
  3. Start sublimetext, click File —> Open Folder menu item, and select your WordPress source code folder.
  4. Click Find —> Find in Files menu item to open find text panel.
  5. Now you will find a popup panel at sublimetext window bottom, input text Vary: Accept-Encoding, Cookie in the Find text box, click Find button.
  6. Now you will see the below source code in the above Find Results window.
    C:\WorkSpace\Work\online-backup-code-learner.com\backup_code-learner.com_2019-02-02_00-14-11_MJJZW7PK01\wp-content\plugins\wp-super-cache\wp-cache-phase2.php:
      168  				}
      169  			} else {
      170: 				header( "Vary: Accept-Encoding, Cookie" );
      171  			}
      172  			if ( defined( 'WPSC_CACHE_CONTROL_HEADER' ) ) {
      ...
     1195  		}
     1196  	} else {
     1197: 		header( 'Vary: Accept-Encoding, Cookie' );
     1198  	}
     1199
  7. Above code shows the PHP file path is \wp-content\plugins\wp-super-cache\wp-cache-phase2.php. That means the Vary header value is added by the WordPress plugin WP Super Cache.
  8. Click the file name in the Find Results window will go to the file wp-cache-phase2.php source code, and you can find below PHP code. You can find the HTTP response header ‘Vary: Accept-Encoding, Cookie’ is hardcoded. This is because wp super cache wants to enable cache for login users with cookie value.

2. How To Remove Cookie From WP Super Cache Vary Header Value.

  1. But you can override it by defining your own Vary header value in constant variable WPSC_VARY_HEADER in the wp-config.php file. The wp-config.php file should be located in the WordPress root folder.
    if ( defined( 'WPSC_VARY_HEADER' ) ) {
        if ( WPSC_VARY_HEADER != '' ) {
        header( 'Vary: ' . WPSC_VARY_HEADER );
        }
    } else {
        header( 'Vary: Accept-Encoding, Cookie' );
    }
  2. Edit the wp-config.php file at the WordPress website root folder, add the below PHP code.
    define('WPSC_VARY_HEADER', 'Accept-Encoding');
  3. Browse the web page again, the Vary header value will not contain Cookie.

3. Why Other Methods Do Not Take Effect.

  1. If you have read the article How To Modify HTTP Response Header In WordPress, you may wonder why that article do not take effect on the WP Super Cache plugin.
  2. This is because WP Super Cache modifies the HTTP Vary header when it generates the static Html web page. This action will be implemented after all those methods. So for the WordPress plugins like WP Super Cache, you must edit it’s source code.

References

  1. How To Modify HTTP Response Header In WordPress
  2. How To Set HTTP Header Vary Value Correct To Make Cache Work Effective
  3. How To View HTTP Headers, Cookies In Google Chrome, Firefox, Internet Explorer
  4. How To Connect Directly To Your Origin Web Server Bypass Intermediary Proxies To View Original HTTP Headers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.