-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrewrite.lua
73 lines (64 loc) · 1.86 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
function is_dir(path)
local x = lighty.stat(path)
return x and lighty.stat(path).is_dir
end
function is_file(path)
local x = lighty.stat(path)
return x and lighty.stat(path).is_file
end
function rewrite(path)
local path = string.gsub(path, "//", "/")
lighty.env["physical.path"] = path
end
function redirect(path)
lighty.header["Location"] = path
return 301
end
-- ininialize variants
domain = lighty.env["uri.authority"]
request_uri = lighty.env["physical.path"]
document_root = lighty.env["physical.doc-root"]
path_raw = lighty.env["uri.path-raw"]
request_uri = string.gsub(request_uri, "/$", "")
-- domain redirect
domain = string.gsub(domain, "kso[-]community", "wps-community")
domain = string.gsub(domain, "wps[-]community[.]com", "wps-community.org")
domain = string.gsub(domain, "www[.]", "")
if domain ~= lighty.env["uri.authority"] then
print("redirect " .. lighty.env["uri.authority"] .. " -> " .. domain)
return redirect(lighty.env["uri.scheme"] .. "://" .. domain .. path_raw)
end
-- deal directory
if is_dir(request_uri) then
if not string.find(path_raw, "/$") then
return redirect(lighty.env["uri.scheme"] .. "://" .. lighty.env["uri.authority"] .. path_raw .. "/")
elseif is_file(request_uri .. "/index.html") then
rewrite(request_uri .. "/index.html")
return
elseif is_file(request_uri .. "/index.htm") then
rewrite(request_uri .. "/index.htm")
return
elseif is_file(request_uri .. "/index.php") then
rewrite(request_uri .. "/index.php")
return
elseif is_file(request_uri .. "/index.rb") then
rewrite(request_uri .. "/index.rb")
return
else
rewrite(document_root .. "/../framework/dircgi.rb")
return
end
end
-- rewrite if not file
if not is_file(request_uri) then
x = string.gsub(request_uri, ".html$", ".rb")
if is_file(x) then
rewrite(x)
return
end
x = request_uri .. ".rb"
if is_file(x) then
rewrite(x)
return
end
end