Hi,
I just write a lua script which makes w3tc work in WordPress. I share it here, it may be useful for someone using lighttpd. If anyone finds a mistake please post here.
Note that i only use w3tc for caching and minifying with default settings so i don't know if it is good for other settings.
First, lighttpd.conf;
$HTTP["url"] =~ "\.(css|js)$" {
expire.url = ( "" => "modification plus 24 hours" )
}
$HTTP["url"] =~ "\.(html|htm|stm)$" {
expire.url = ( "" => "modification plus 60 minutes" )
}
$HTTP["host"] =~ "^(www.yourdomain.com|yourdomain.com)" {
magnet.attract-physical-path-to = ( "/absolute/path/w3tc.lua" )
}
Second, the lua script;
function explode(d,p)
local t, ll
t={}
ll=0
if(#p == 1) then return p end
while true do
l=string.find(p,d,ll+1,true)
if l~=nil then
table.insert(t, string.sub(p,ll,l-1))
ll=l+1
else
table.insert(t, string.sub(p,ll))
break
end
end
return t
end
function serve_headers(wh)
lighty.header["X-Powered-By"] = "W3 Total Cache/0.8.5.2"
lighty.header["Pragma"] = "public"
if(wh == 1) then
lighty.header["Vary"] = "Accept-Encoding, Cookie"
lighty.header["X-Pingback"] = lighty.env["uri.scheme"] .. "://" .. lighty.env["uri.authority"] .. "/xmlrpc.php"
else
lighty.header["Vary"] = "Accept-Encoding"
end
cachecontrol = lighty.header["Cache-Control"] or ""
if(cachecontrol == "") then
lighty.header["Cache-Control"] = "public, must-revalidate, proxy-revalidate"
else
lighty.header["Cache-Control"] = cachecontrol .. ", public, must-revalidate, proxy-revalidate"
end
end
function serve_html(cached_page,wh)
if (lighty.stat(cached_page)) then
if(wh == 1) then
serve_headers(1)
else
serve_headers(2)
end
lighty.env["physical.path"] = cached_page
return true
else
return false
end
end
function serve_gzip(cached_page,wh)
if (lighty.stat(cached_page .. ".gzip")) then
lighty.header["Content-Encoding"] = "gzip"
if(wh == 1) then
lighty.header["Content-Type"] = "text/html"
serve_headers(1)
else
if(string.match(lighty.env["physical.rel-path"], "%.js$")) then
lighty.header["Content-Type"] = "application/x-javascript"
else
lighty.header["Content-Type"] = "text/css"
end
serve_headers(2)
end
lighty.env["physical.path"] = cached_page .. ".gzip"
return true
else
return false
end
end
if (string.match(lighty.env["physical.rel-path"], "/wp%-content/w3tc/min/")) then
attr = lighty.stat(lighty.env["physical.path"] .. ".gzip")
if (not attr) then
attr = lighty.stat(lighty.env["physical.path"])
if (not attr) then
minfile = string.gsub(lighty.env["uri.path"], "/wp%-content/w3tc/min/","")
gggt = explode(".",minfile)
lighty.env["uri.path"] = "/wp-content/w3tc/min/index.php"
lighty.env["uri.query"] = "gg=" .. gggt[1] .."&g=" .. gggt[2] .."&t=" .. gggt[3]
lighty.env["physical.rel-path"] = lighty.env["uri.path"]
lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
end
else
accept_encoding = lighty.request["Accept-Encoding"] or "no_acceptance"
cached_page = lighty.env["physical.path"]
if (string.find(accept_encoding, "gzip")) then
if not serve_gzip(cached_page,2) then serve_html(cached_page,2) end
else
serve_html(cached_page,2)
end
end
else
attr = lighty.stat(lighty.env["physical.path"])
if (not attr) then
lighty.env["uri.path"] = "/index.php"
lighty.env["physical.rel-path"] = lighty.env["uri.path"]
lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
user_query = lighty.env["uri.query"] or ""
query_condition = (user_query == "")
user_cookie = lighty.request["Cookie"] or ""
cookie_condition = not (string.find(user_cookie, ".*comment_author.*") or string.find(user_cookie, ".*wordpress.*") or string.find(user_cookie, ".*wp-postpass_.*"))
user_method = lighty.env["request.method"] or ""
method_condition = (user_method ~= "POST")
if (query_condition and cookie_condition and method_condition) then
accept_encoding = lighty.request["Accept-Encoding"] or "no_acceptance"
cached_page = lighty.env["physical.doc-root"] .. "/wp-content/w3tc/pgcache/" .. lighty.env["request.uri"] .. "/_default_.html"
cached_page = string.gsub(cached_page, "//", "/")
if (string.find(accept_encoding, "gzip")) then
if not serve_gzip(cached_page,1) then serve_html(cached_page,1) end
else
serve_html(cached_page,1)
end
end
end
end