How to Fix "an error occurred while processing this directive" Message

Problem

After enabling LiteMage, the store displays “an error occurred while processing this directive” wherever an ESI block should be. This occurs in all ESI blocks, and as we see when we turn on LiteMage's debug log, none of the ESI requests have been sent to the Magento backend after the main page request.

Solution

Determine whether the store uses a page optimization plugin. If so, this could be the source of the problem, as it changes the html after LiteMage injects <ESI:include> for hole punching.

If no such optimization plugin is in use, another common cause is rewritten URLs. If something rewrites the URLs for <ESI:include> directives, that will break LiteMage.

To investigate, we need to turn on the rewrite log and the server debug log. If https is used for the store, enable the rewrite log on both 80 and 443 virtual hosts.

  1. Add the following to the virtual host and restart the web server: RewriteLogLevel 9
  2. Enable LSWS debug log: ps -ef | grep litespeed to check the child process PID, then kill -USR2 <PID> to enable the debug log.
  3. Rename the old error log file: mv error_log error_log.bak1
  4. Run a URL test from the browser to trigger the error.
  5. Rename the new error log file: mv error_log log2check
  6. Turn off the debug log, by running the same command again: kill -USR2 <PID>
  7. Look at the saved debug log: vi log2check

We see some rewrite rules have redirected /litemage/esi, which causes ESI blocks not to work in LiteMage:

Error log:
2017-05-11 20:57:23.959 [INFO] [108.162.219.111:32659#APVH_82.147.14.70:S-2#APVH_82.147.14.70:443_staging.somedomain.] 
[REWRITE] Cond: Match 
'/litemage/esi/getBlock/t/cart/bi/cart_sidebar/h/D/extra/YToxOntzOjk6ImFqYXhibG9jayI7YjoxO30,/s/1/dp/em0141/dt/em0141_shoes' with pattern '(.*)/$', result: -1
2017-05-11 20:57:23.959 [INFO] [108.162.219.111:32659#APVH_82.147.14.70:S-2#APVH_82.147.14.70:443_staging.somedomain.] [REWRITE] Source URI: 
'litemage/esi/getBlock/t/cart/bi/cart_sidebar/h/D/extra/YToxOntzOjk6ImFqYXhibG9jayI7YjoxO30,/s/1/dp/em0141/dt/em0141_shoes/' => 
Result URI: 'http://staging.somedomain.co.uk/litemage/esi/getBlock/t/cart/bi/cart_sidebar/h/D/extra/YToxOntzOjk6ImFqYXhibG9jayI7YjoxO30,/s/1/dp/em0141/dt/em0141_shoes/'
2017-05-11 20:57:23.959 [INFO] [108.162.219.111:32659#APVH_82.147.14.70:S-2#APVH_82.147.14.70:443_staging.somedomain.] [REWRITE] Last Rule, stop!

We look in .htaccess and identify the following faulty rewrite rules:

## Custom Selesti rewrites

#301 redirect if pagination is on page 1 in query string
RewriteCond %{QUERY_STRING} p=1
RewriteRule ^(.*)$ %{REQUEST_URI}? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.gif|\.png|\.jpg|\.jpeg|\.css|\.js)$ [NC]
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteCond %{HTTP:X-REQUESTED-WITH} !^(XMLHttpRequest)$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]

RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_URI} !^/(media|skin|js|customer|checkout|sgps|ajaxcart|themeframework|wishlist|)/
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteCond %{HTTP:X-REQUESTED-WITH} !^(XMLHttpRequest)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*) ${lc:$1} [R=301,L]

We can solve this problem by adding conditions to those rules that will exclude /litemage/esi:

## Custom Selesti rewrites

#301 redirect if pagination is on page 1 in query string
RewriteCond %{QUERY_STRING} p=1
RewriteRule ^(.*)$ %{REQUEST_URI}? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.gif|\.png|\.jpg|\.jpeg|\.css|\.js)$ [NC]
RewriteCond %{REQUEST_URI} !litemage
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteCond %{HTTP:X-REQUESTED-WITH} !^(XMLHttpRequest)$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]

RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_URI} !^/(media|skin|js|customer|checkout|sgps|ajaxcart|themeframework|wishlist|)/
RewriteCond %{REQUEST_URI} !litemage
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteCond %{HTTP:X-REQUESTED-WITH} !^(XMLHttpRequest)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*) ${lc:$1} [R=301,L]

Problem solved. The error message is gone, and the ESI requests are being correctly sent to the Magento backend.