chesio / bc-cache

Simple full page cache plugin for WordPress inspired by Cachify
The Unlicense
9 stars 0 forks source link

POST requests to root directory are served from cache #6

Closed chesio closed 6 years ago

chesio commented 6 years ago

I don't fully understand what is going on, but the problem seems to be related to the internal subrequest to index.php file that is triggered via WordPress rewrite rules in .htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

The subrequest apparently does not retain original %{REQUEST_METHOD}, so this BC Cache condition fails:

RewriteCond %{REQUEST_METHOD} !=POST

Also, somehow the cache file is matched, although it shouldn't because %{ENV:BC_CACHE_PATH} should be empty and thus there should be no directory separator before %{ENV:BC_CACHE_FILE}:

RewriteCond %{DOCUMENT_ROOT}/wordpress/wp-content/cache/bc-cache/%{ENV:BC_CACHE_HOST}%{ENV:BC_CACHE_PATH}%{ENV:BC_CACHE_FILE} -f

I don't know for sure, but my guess is that:

  1. Either the original %{REQUEST_URI} is retained in the subrequest and so %{ENV:BC_CACHE_PATH} is constructed correctly.
  2. Or the original %{ENV:BC_CACHE_PATH} is retained in the subrequest.

Anyway, the problem can be fixed with [NS] ("nosubreq" flag) added to rewrite rule:

RewriteRule .* /wordpress/wp-content/cache/bc-cache/%{ENV:BC_CACHE_HOST}%{ENV:BC_CACHE_PATH}%{ENV:BC_CACHE_FILE} [L,NS]