Diff

plugins/mod_http.lua @ 6504:e1659f32852e

mod_http: For URLs that end with / or wildcard handlers, add a low-priority redirect from without to with slash
author Kim Alvefur <zash@zash.se>
date Tue, 04 Nov 2014 17:48:17 +0100
parent 6086:3b4fde51fa25
child 6530:35ebcb733c4c
line wrap: on
line diff
--- a/plugins/mod_http.lua	Mon Oct 27 11:12:08 2014 +0100
+++ b/plugins/mod_http.lua	Tue Nov 04 17:48:17 2014 +0100
@@ -45,6 +45,11 @@
 		:gsub("%$(%w+)", { host = host_module.host });
 end
 
+local function redir_handler(event)
+	event.response.headers.location = event.request.path.."/";
+	return 301;
+end
+
 local ports_by_scheme = { http = 80, https = 443, };
 
 -- Helper to deduce a module's external URL
@@ -99,6 +104,9 @@
 						local path = event.request.path:sub(base_path_len);
 						return _handler(event, path);
 					end;
+					module:hook_object_event(server, event_name:sub(1, -3), redir_handler, -1);
+				elseif event_name:sub(-1, -1) == "/" then
+					module:hook_object_event(server, event_name:sub(1, -2), redir_handler, -1);
 				end
 				if not app_handlers[event_name] then
 					app_handlers[event_name] = handler;