-- $Id$ -- Garrett Albright's Ultimate Lua script for running Drupal on Lighty! -- May require minor customization - be sure to read the comments at the start -- of each section below (comment lines start with two hyphens, just like these -- lines). -- For more information on how to use this script on your server, see: -- http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModMagnet -- Remove "www" from URLs. Note that unlike the .htaccess file that comes with -- Drupal, you don't have to edit this to add your site's/sites' URL/URLs - we -- can determine that automatically. See also: http://drupal.org/node/352180 -- Delete the next line to activate this. --[[ -- Match "www." at the beginning of the URL. -- Note that Lua's matching system is inspired by standard regular -- expressions, but is not a drop-in replacement. In this case it's close -- enough, though. See: http://www.lua.org/manual/5.1/manual.html#5.4.1 -- The match function returns nil when there's no match. if (lighty.env['uri.authority']:match('^www%.') ~= nil) then -- Rebuild the URL without the "www." and pass it as the "Location" header. -- Note that Lua's string counting functions are 1-based (the first character -- is at position 1, not position 0 as in most other languages), so the 5 -- parameter is correct for the sub() function below. lighty.header['Location'] = lighty.env['uri.scheme'] .. '://' .. lighty.env['uri.authority']:sub(5) .. lighty.env['request.orig-uri'] -- Return HTTP status code 301. return 301 end --]] -- Add "www" to URLs. Read the comments in the "Remove 'www'" section above for -- more info - much of it could be repeated here. -- Delete the next line to activate this. --[[ if (lighty.env['uri.authority']:match('^www%.') == nil) then -- Rebuild URL, adding "www.", and pass it in as the "Location" header. lighty.header['Location'] = lighty.env['uri.scheme'] .. '://www.' .. lighty.env['uri.authority'] .. lighty.env['request.orig-uri'] return 301 end --]] -- Clean URL support. -- Only kick in if the file Lighty will look for doesn't exist (lighty.stat() -- returns false). if (not lighty.stat(lighty.env['physical.path'])) then -- IMPORTANT: If your Drupal installation is in a subdirectory - for example, -- at http://example.com/drupal/ instead of just http://example.com/ - set the -- d_path variable below to the path to that directory, with a slash at the -- beginning AND at the ending of the path. For the example above, you -- would use: -- local d_path = '/drupal/' -- If your Drupal installation is at your web site's root - for example, just -- at http://example.com/ - set this variable to a single slash: -- local d_path = '/' local d_path = '/' local path_trimmed = lighty.env['uri.path']:sub(d_path:len()); -- Have something in the cookie to avoid having to check for nil twice later if (lighty.request['Cookie'] == nil) then lighty.request['Cookie'] = '' end -- Do we want to look for this item in Boost's cache? -- Lua's patterns don't support an "or" operator, so we can't do a pattern -- like '^/(admin|cache|etc)'. Instead, this kludge. for idx, path in ipairs({ '^/admin', '^/cache', '^/misc', '^/modules', '^/sites', '^/system', '^/openid', '^/themes', '^/node/add', '^/comment/reply', '^/edit', '^/user$', '^/user/[^%d]', }) do if path_trimmed:match(path) then bad_path = true break end end -- Test for the test page if (path_trimmed == '/boost-gzip-cookie-test.html') then lighty.env['physical.path'] = lighty.env['physical.doc-root'] .. d_path .. 'cache/perm/boost-gzip-cookie-test.html.gz' lighty.env['uri.path'] = d_path .. 'cache/perm/boost-gzip-cookie-test.html.gz' lighty.header['Content-Type'] = 'text/html' lighty.header['Content-Encoding'] = 'gzip' lighty.header['X-Boost-Path'] = lighty.env['uri.path'] elseif (lighty.env['request.method'] == 'GET' and lighty.env['uri.scheme'] ~= 'https' and lighty.request['Cookie']:find('DRUPAL_UID', 1, true) == nil and bad_path ~= true) then -- Start searching the cache for hits local cache_path = d_path .. 'cache/normal/' .. lighty.env['uri.authority'] .. path_trimmed .. '_' .. (lighty.env['uri.query'] == nil and '' or lighty.env['uri.query']) local extensions = { ['.css'] = 'text/css', ['.js'] = 'text/javascript', ['.html'] = 'text/html', ['.xml'] = 'text/xml', ['.json'] = 'text/javascript', } -- Try gzipped files first, if the browser will accept it. if ((lighty.request['Accept-Encoding'] ~= nil and lighty.request['Accept-Encoding']:find('gzip', 1, true)) or lighty.request['Cookie']:find('boost-gzip', 1, true)) then for ext, type in pairs(extensions) do local suffixed = cache_path .. ext .. '.gz' local full_path = lighty.env['physical.doc-root'] .. suffixed if (lighty.stat(full_path)) then -- Hit lighty.env['physical.path'] = full_path lighty.env['uri.path'] = suffixed lighty.header['Content-Type'] = type lighty.header['X-Boost-Path'] = full_path lighty.header['Content-Encoding'] = 'gzip' break end end end if lighty.header['X-Boost-Path'] == nil then -- Now try without gzip for ext, type in pairs(extensions) do local suffixed = cache_path .. ext local full_path = lighty.env['physical.doc-root'] .. suffixed if (lighty.stat(full_path)) then -- Hit lighty.env['physical.path'] = full_path lighty.env['uri.path'] = suffixed lighty.header['Content-Type'] = type lighty.header['X-Boost-Path'] = full_path break end end end end if lighty.header['X-Boost-Path'] == nil then -- The request wasn't Boostable. Do standard Drupal query string building. -- Rewrite the query part of the URI (or create it if there isn't one) to -- append "q=" (while stripping away the path to the Drupal installation -- if it's in there). Note that the "and/or" structure below is Lua's -- equivalent of the tertiary operator ($foo = $bar ? baz() : qux()). lighty.header['X-Boost-Path'] = 'miss' lighty.env['uri.query'] = (lighty.env['uri.query'] == nil and '' or lighty.env['uri.query'] .. '&') .. 'q=' .. lighty.env['uri.path']:sub(d_path:len()) lighty.env['uri.path'] = d_path .. 'index.php' end lighty.env['physical.rel-path'] = lighty.env['uri.path'] lighty.env['physical.path'] = lighty.env['physical.doc-root'] .. lighty.env['physical.rel-path'] end