Created
October 2, 2011 23:19
-
-
Save sxua/1258096 to your computer and use it in GitHub Desktop.
Lighttpd rewrite conditions written in Lua for WP Super Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function serve_html(cached_page) | |
if (lighty.stat(cached_page)) then | |
lighty.env["physical.path"] = cached_page | |
return true | |
else | |
return false | |
end | |
end | |
function serve_gzip(cached_page) | |
if (lighty.stat(cached_page .. ".gz")) then | |
lighty.header["Content-Encoding"] = "gzip" | |
lighty.header["Content-Type"] = "" | |
lighty.header["X-Powered-By"] = "Lua Super Cache" | |
lighty.env["physical.path"] = cached_page .. ".gz" | |
return true | |
else | |
return false | |
end | |
end | |
function file_exists(path) | |
local attr = lighty.stat(path) | |
if (attr) then | |
return true | |
else | |
return false | |
end | |
end | |
function go_php() | |
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"] | |
end | |
function is_root() | |
if (lighty.env["physical.path"] == lighty.env["physical.doc-root"] .. "/") then | |
return true | |
else | |
return false | |
end | |
end | |
if (is_root() or not file_exists(lighty.env["physical.path"])) then | |
if (string.match(lighty.env["uri.path"],"^/([%w%_%-]+)/(wp%-.*)")) then | |
n, a = string.match(lighty.env["uri.path"], "^/([%w%_%-]+)/(wp%-.*)") | |
if a then | |
lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. "/" .. a | |
else | |
n, a = string.match(lighty.env["uri.path"], "^([%w%_%-%/]+)(.*%.php)$") | |
if a then | |
lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. "/" .. a | |
end | |
end | |
else | |
query = (lighty.env["uri.query"] and string.find(lighty.env["uri.query"], ".*s=.*")) | |
post = (lighty.env["request.method"] == "POST") | |
if (not query and not post) then | |
accept_encoding = lighty.request["Accept-Encoding"] or "no_acceptance" | |
cached_page = lighty.env["physical.doc-root"] .. "/wp-content/cache/supercache/" .. lighty.request["Host"] .. lighty.env["request.uri"] .. "/index.html" | |
cached_page = string.gsub(cached_page, "//", "/") | |
if (file_exists(cached_page)) then | |
if (string.find(accept_encoding, "gzip")) then | |
if (not serve_gzip(cached_page)) then | |
serve_html(cached_page) | |
end | |
else | |
serve_html(cached_page) | |
end | |
else | |
go_php() | |
end | |
else | |
go_php() | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment