Search code examples
.htaccessmod-rewritenginxurl-rewritinglighttpd

Convert nginx to lighttpd rewrite rule


I need to change nginx server to lighttpd server. The CMS should be run on a web server that supports URL rewriting. Working nginx config is provided below. The problem is to covert nginx configuration to lighttpd configuration.

server {

        # The port to listen on
        listen 8086;

        # The host to listen on
        #server_name localhost

        # The root directory
        root /storage/emulated/0/htdocs/web/;

        # Enable directory listing
        #autoindex on;
        autoindex on;

        # Don't show any server tokens
        server_tokens off;

        # The default indexes to look for
        index  index.html index.htm index.php;


        location / { try_files $uri /index.php?$args; } 
        location /api/authorize { try_files $uri /api/authorize/index.php?args; } 
        location /api { try_files $uri /api/index.php?$args; } 
        location /install { try_files $uri /install/index.php?$args; } 
        location /maint { try_files $uri /maint/index.php?$args; } 
        location /maintenance { try_files $uri /index.php?$args; }

        }

Solution

  • I dont quite understand what you mean but you can try something like the following example lighttpd rewrite rules:

    url.rewrite-once
    

    Rewrites a set of URLs internally in the webserver BEFORE they are handled.

    e.g.

    url.rewrite-once = ( "<regex>" => "<relative-uri>" )
    

    or for multiple rules..

    url.rewrite-once = ( 
      "<regex1>" => "<relative-uri1>",
      "<regex2>" => "<relative-uri2>" 
    )
    url.rewrite-repeat
    

    Rewrites a set of URLs internally in the webserver BEFORE they are handled

    e.g.

    url.rewrite-repeat = ( "<regex>" => "<relative-uri>" )
    

    The difference between these options is that, while url.rewrite-repeat allows for applying multiple (seperately defined) rewrite rules in a row, url.rewrite-once will cause further rewrite rules to be skipped if the expression was matched. As such, url.rewrite-once behaves like Apaches' RewriteRule ... [L]: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule

    The options url.rewrite and url.rewrite-final were mapped to url.rewrite-once in 1.3.16.

    url.rewrite-[repeat-]if-not-file
    

    New: For the 1.4.x branch as of 1.4.24 or r2647 from svn:

    Rewrites a set of URLs internally in the webserver BEFORE they are handled and checks that files do not exist.

    Take examples from above, this is to mimic Apache´s "!-f" RewriteRule. Please note this does not work for directories, pipes, sockets or alike.

    Where do I want to use this? Maybe with e.g. Drupal backend, where mod_magnet (has an Apache´s -f and -d solution) might not be handy or simply "too much" for just this kind of rewrites.

    Regular Expressions Patterns ("wildcards") are matched against a string Special characters (see [http://www.regular-expressions.info/reference.html] for reference):

    • . (full stop) - match any character
    • * (asterisk) - match zero or more of the previous symbol
    • + (plus) - match one or more of the previous symbol
    • ? (question) - match zero or one of the previous symbol
    • \? (backslash-something) - match special characters
    • ^ (caret) - match the start of a string
    • $ (dollar) - match the end of a string
    • [set] - match any one of the symbols inside the square braces.
    • [^set] - match any symbol that is NOT inside the square braces.
    • (pattern) - grouping, remember what the pattern matched as a special variable
    • {n,m} - from n to m times matching the previous character (m could be omitted to mean >=n times)
    • (?!expression) - match anything BUT expression at the current position. Example: "^(/(?!(favicon.ico$|js/|images/)).*)" => "/fgci/$1"l Normal alphanumeric characters are treated as normal Replacement Patterns If the matched regex contains groups in parentheses, $1..$9 in the replacement refer to the captured text in the matching group "$1" meaning the first group, "$2" the second, and so on.

    Note that % replacements (like %1, %2, %0, etc.) in url.rewrite-* targets are permitted, but do not have the meaning they would have in evhost.path-pattern. If url.rewrite-* is specified within a regex conditional, % patterns are replaced by the corresponding groups from the condition regex. %1 is replaced with the first subexpression, %2 with the second, etc. %0 is replaced by the entire substring matching the regexp. See below for an example using "%0”.

    Examples The regex is matching the full REQUEST_URI which is supplied by the user including

    query-string.

    # the following example, is, however just simulating vhost by rewrite
    # * you can never change document-root by mod_rewrite
    # use mod_*host instead to make real mass-vhost
    
    server.document-root = "/www/htdocs/" 
    $HTTP["host"] =~ "^.*\.([^.]+\.com)$" {
      url.rewrite-once = ( "^/(.*)" => "/%0/$1" )
    }
    
    # request:        http://any.domain.com/url/ 
    # before rewrite: REQUEST_URI="/www/htdocs/url/" 
    # and DOCUMENT_ROOT="/www/htdocs/" %0="any.domain.com" $1="url/" 
    # after rewrite:  REQUEST_URI="/www/htdocs/any.domain.com/url/" 
    # still, you have DOCUMENT_ROOT=/www/htdocs/
    
    # please note, that we have two regular expressions: the one which 
    # $HTTP["host"] is been compared with, and the one of the rewrite  rule.
    # the numbered subexpressions available to build the relative uri are
    # being prefixed by '%' for subexpressions of the first regular expression 
    # match and by '$' for subexpressions of the second one.
    # subexpression 0 interpolates the whole matching string: %0 for the whole
    # string matching the conditional, and $0 for the whole string matching the
    # rewrite rule.
    
    # if the rewrite rule is not included in a conditional 
    # block, only the '$' prefixed variables are available.
    
    url.rewrite-once = ( "^/id/([0-9]+)$" => "/index.php?id=$1",
                         "^/link/([a-zA-Z]+)" => "/index.php?link=$1" )
    

    With mod_redirect Rewrite rules always execute before redirect rules. This is true regardless of the order of module loading or the order of rules in the configuration (lighttpd v1.4.13). However, mod_rewrite provides a mechanism to pass URLs through unmangled: specify "$0”as the rule target, but be sure that the rule matches the entire string since $0 is the entire matched string.

    e.g.

    url.rewrite-once = (
        "^/foo"  => "$0",
        "^/(.*)" => "/handler/$1" 
    )
    
    url.redirect = (
        "^/foo"  => "http://foo.bar/" 
     )
    

    Since version 1.4.40, an alternative is to specify a blank target to the rewrite rule. This will cause the matched rule to leave the url unmodified, and will skip any further rewrite rules.

    url.rewrite-once = (
        "^/foo"  => "",   # instead of (nonsensical) blank url, the url  will not be modified
        "^/(.*)" => "/handler/$1" 
    )
    

    Workaround for "File name too long" on Windows While running Lighttpd on Windows you may get 500 Internal Server Error if computed filename is longer than 255 symbols. In error log it will be (response.c.537) file not found ... or so: File name too long /very_looooong_path ->. As workaround you can use mod_rewrite to avoid this error.

    server.modules += ("mod_rewrite")
    url.rewrite-once = ( ".{250,}" => "/toolong.php" )
    

    If error handler is PHP, $_SERVER['REQUEST_URI'] will contain full URI.

    Passing / Matching the Query string (GET variables) If you wanna pass the Query String (?foo=bar) to the rewrite destination you have to explicitly match it:

    url.rewrite-once = (
    "^/news/([^\?]+)(\?(.*))?" => "/news.php?title=$1&$3" 
    )
    

    Documentation