-
Notifications
You must be signed in to change notification settings - Fork 1
/
rewrite.lua
266 lines (242 loc) · 11.6 KB
/
rewrite.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
--[[
rewrite.lua
-------------------------------------------------------------------------------
Rewrite rules for a Wordpress 3.x installation on top of a lighttpd web server.
This LUA script comes in very handy if your using the following plugins in
your Wordpress installation:
* wp-super-cache (http://ocaoimh.ie/wp-super-cache/)
* WP-touch (http://bravenewcode.com/wptouch)
This LUA script is based on the great work of Giovanni Intini:
http://tempe.st/2008/05/lightning-speed-wordpress-with-lighttpd-and-supercache-part-ii/
-------------------------------------------------------------------------------
Copyright (C) 2008-2011 by Giovanni Intini, Jean Pierre Wenzel, Eric Chamberlain
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------------
--]]
-- ----------------------------------------------------------------------------
-- CONFIGURATION
enableUserAgentCheck = true
-- Taken from the WPSuperCache mod_rewrite rules
-- RewriteCond %{HTTP_USER_AGENT} !^.*(2.0\ MMP|240x320|400X240|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|Googlebot-Mobile|hiptop|IEMobile|KYOCERA/WX310K|LG/U990|MIDP-2.|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|PlayStation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|SHG-i900|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|webOS|Windows\ CE|WinWAP|YahooSeeker/M1A1-R2D2|iPhone|iPod|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800).* [NC]
local userAgentsNoCaching = {
"2.0 MMP",
"240x320",
"400X240",
"AvantGo",
"BlackBerry",
"Blazer",
"Cellphone",
"Danger",
"DoCoMo",
"Elaine/3.0",
"EudoraWeb",
"Googlebot-Mobile",
"hiptop",
"IEMobile",
"KYOCERA/WX310K",
"LG/U990",
"MIDP-2.",
"MMEF20",
"MOT-V",
"NetFront",
"Newt",
"Nintendo Wii",
"Nitro",
"Nokia",
"Opera Mini",
"Palm",
"PlayStation Portable",
"portalmmm",
"Proxinet",
"ProxiNet",
"SHARP-TQ-GX10",
"SHG-i900",
"Small",
"SonyEricsson",
"Symbian OS",
"SymbianOS",
"TS21i-10",
"UP.Browser",
"UP.Link",
"webOS",
"Windows CE",
"WinWAP",
"YahooSeeker/M1A1-R2D2",
"iPhone",
"iPod",
"Android",
"BlackBerry9530",
"LG-TU915 Obigo",
"LGE VX",
"Nokia5800"
}
-- RewriteCond %{HTTP_user_agent} !^(w3c\ |w3c-|acs-|alav|alca|amoi|audi|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|htc_|inno|ipaq|ipod|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|lg/u|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|palm|pana|pant|phil|play|port|prox|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda\ |xda-).* [NC]
local userAgentsStartWithNoCaching = {
"w3c ",
"w3c-",
"acs-",
"alav",
"alca",
"amoi",
"audi",
"avan",
"benq",
"bird",
"blac",
"blaz",
"brew",
"cell",
"cldc",
"cmd-",
"dang",
"doco",
"eric",
"hipt",
"htc_",
"inno",
"ipaq",
"ipod",
"jigs",
"kddi",
"keji",
"leno",
"lg-c",
"lg-d",
"lg-g",
"lge-",
"lg/u",
"maui",
"maxo",
"midp",
"mits",
"mmef",
"mobi",
"mot-",
"moto",
"mwbp",
"nec-",
"newt",
"noki",
"palm",
"pana",
"pant",
"phil",
"play",
"port",
"prox",
"qwap",
"sage",
"sams",
"sany",
"sch-",
"sec-",
"send",
"seri",
"sgh-",
"shar",
"sie-",
"siem",
"smal",
"smar",
"sony",
"sph-",
"symb",
"t-mo",
"teli",
"tim-",
"tosh",
"tsm-",
"upg1",
"upsi",
"vk-v",
"voda",
"wap-",
"wapa",
"wapi",
"wapp",
"wapr",
"webc",
"winw",
"winw",
"xda ",
"xda-"
}
-- Requests from Linklift's Linkbot HTTP crawler should not be cached.
table.insert(userAgentsStartWithNoCaching, "linkbot")
table.insert(userAgentsStartWithNoCaching, "linklift")
-- ----------------------------------------------------------------------------
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.env["physical.path"] = cached_page .. ".gz"
return true
else
return false
end
end
function string.starts(String,Start)
return string.sub(String,1,string.len(Start)) == Start
end
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"]
-- Sending a HTTP query? => no caching
query_condition = not (lighty.env["uri.query"] and string.find(lighty.env["uri.query"], ".*s=.*"))
-- Have a cookie? => no caching
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_.*"))
sendCachedFile = true
if (enableUserAgentCheck) then
-- Check if request comes from a mobile device or bot => no caching then, either.
userAgent = lighty.request["User-Agent"]
if (not nil == userAgent) then
userAgent = string.lower(userAgent)
for i, v in ipairs(userAgentsNoCaching) do
if (string.find(v, userAgent)) then
sendCachedFile = false
break
end
end
if (sendCachedFile) then
for i, v in ipairs(userAgentsStartWithNoCaching) do
if (string.starts(v, userAgent)) then
sendCachedFile = false
break
end
end
end
end
end
if (query_condition and cookie_condition and sendCachedFile) 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 (string.find(accept_encoding, "gzip")) then
if not serve_gzip(cached_page) then serve_html(cached_page) end
else
serve_html(cached_page)
end
end
end