Online Chat
 Call Us: 
1-877-744-1221
Browse Submit a Ticket
 
Advanced Search
Tools
Rss Categories

URL rewrite support for Lighttpd server

Author: Edward Hardin Reference Number: AA-00382 Views: 9031 Last Updated: 08/04/2009 04:12 PM 0 Rating/ Voters

While Lighttpd features a rewrite module, it is not an equivalent of Apache's mod_rewrite. Full mod_rewrite functionalities are spread amongst Lighttpd's mod_rewrite, mod_magnet and mod_proxy.

KnowledgeBase Manager Pro, however, needs mod_magnet to redirect requests in order to work with SEO- and user-friendly URLs.

Requirements

lighttpd 1.4.12 or higher
lua >= 5.1

Installation

mod_magnet needs a Lighttpd which is compiled with the lua-support option ( --with-lua). Lua 5.1 or higher are required by the module.

To enable mod_magnet, add following line to the lighttpd.conf file (it is usually located in the /etc/lighttpd/ directory):

server.modules = ( ..., "mod_magnet", ... )

Example with other modules enabled (some are disabled):

lighttpd.conf
server.modules              = (
            "mod_access",
            "mod_alias",
            "mod_accesslog",
            "mod_magnet",
            "mod_compress",
#           "mod_status",
#           "mod_evhost",
#           "mod_usertrack",
#           "mod_rrdtool",
#           "mod_webdav",
#           "mod_expire",
#           "mod_flv_streaming",
#           "mod_evasive"
)

Restart your Lighttpd:

service lighttpd restart

If Lighttpd fails to start after that, you need to install mod_magnet.

For example, do the following to install mod_magnet on Ubuntu:

~# apt-get install lighttpd-mod-magnet
~# lighty-enable-mod magnet
~# /etc/init.d/lighttpd force-reload

Add reference to Lua script that will handle URLs rewriting.

lighttpd.conf
$HTTP["url"] =~ "^/kmp/" {
    index-file.names = ( "index.php" )
    magnet.attract-physical-path-to = ( "/etc/lighttpd/kmp.lua" )
}

Where
/kmp/ - is path from web root to KnowledgeBase Manager Pro installation on your server.
/etc/lighttpd/kmp.lua - is path to Lua script for KnowledgeBase Manager Pro.

Create /etc/lighttpd/kmp.lua script file with following contents:

kmp.lua
function file_exists(path)
  local attr = lighty.stat(path)
    if (attr) then
          return true
    else
          return false
    end
end

function removePrefix(str, prefix)
      return str:sub(1,#prefix+1) == prefix.."/" and str:sub(#prefix+2)
end

-- prefix without the trailing slash
local prefix = '/kmp'

function whichDir(prefix)
local uri = lighty.env["uri.path"]:sub(#prefix+2)
local admin = 'admin/'
local hosts = 'hosts/'
local hosted = 'hosted/'
local preview = 'preview/'
b = uri:sub(1,#admin)
if (b == admin) then
    return admin
        elseif (b == hosts) then b = uri:sub(#hosts,#hosted-#hosts)
        if (b == hosts) then return hosts elseif (b == (preview:sub(1,#preview-1))) then return preview
        end
    end
return ''
end

function hastrailingslash()
return "/" == lighty.env["uri.path"]:sub(#lighty.env["uri.path"]-1)
end

      if (not file_exists(lighty.env["physical.path"]) and not hastrailingslash()) then
          request_uri = removePrefix(lighty.env["uri.path"], prefix)
          if request_uri then
            lighty.env["uri.path"]          = prefix .. "/" .. whichDir(prefix) .. "index.php"
            print (prefix .. "/" .. whichDir(prefix) .. "index.php")
            local uriquery = lighty.env["uri.query"] or ""
            lighty.env["uri.query"] = uriquery .. request_uri:sub(#whichDir(prefix)+1)
            lighty.env["physical.rel-path"] = lighty.env["uri.path"]
            lighty.env["request.orig-uri"]  = lighty.env["request.uri"]
            lighty.env["physical.path"]     = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
            print (lighty.env["uri.scheme"] .. "://" .. lighty.env["uri.authority"] .. lighty.env["uri.path"] .. "?" .. lighty.env["uri.query"])
          end
      end

Replace path in local prefix = '/kmp' with actual path from web root to KnowledgeBase Manager Pro on your server.

Restart your server to apply changes.