项目

通用

个人资料

操作

如何将HTTP请求重定向到HTTPS

更多详情和示例请参见mod_redirect

重定向所有请求

$HTTP["scheme"] == "http" {
    url.redirect = ("" => "https://${url.authority}${url.path}${qsa}")
    #url.redirect-code = 308  # (before lighttpd 1.4.75)
}

重定向所有请求,但排除来自IPv4或IPv6本地主机的连接

$HTTP["scheme"] == "http" {
    $HTTP["remote-ip"] != "127.0.0.1" {
        $HTTP["remote-ip"] != "[::1]" {
            url.redirect = ("" => "https://${url.authority}${url.path}${qsa}")
            #url.redirect-code = 308  # (before lighttpd 1.4.75)
        }
    }
}

将特定URL从HTTP重定向到HTTPS

$HTTP["scheme"] == "http" {
    url.redirect = ("^/phpmyadmin/" => "https://${url.authority}${url.path}${qsa}")
    #url.redirect-code = 308  # (before lighttpd 1.4.75)
}

将特定虚拟主机和URL从HTTP重定向到HTTPS

$HTTP["scheme"] == "http" {
    $HTTP["host"] == "sth.example.com" {
        url.redirect = ("^/phpmyadmin/.*" => "https://sth.example.com$0")
        #url.redirect-code = 308  # (before lighttpd 1.4.75)
    }
}

将所有来自非标准端口(即非80端口)的HTTP请求重定向到HTTPS

https://stackoverflow.com/questions/62109311/lighttpd-redirect-from-custom-http-port-81-to-https-port-443

$HTTP["scheme"] == "http" {
    $HTTP["host"] =~ "^(.*?)(:\d+)?$" {  ## %1 contains hostname without port
        url.redirect = ("" => "https://%1${url.path}${qsa}")
        #url.redirect-code = 308  # (before lighttpd 1.4.75)
    }
}

Lighttpd 1.4.50之前的Lighttpd 1.4.x早期版本可能需要将上述url.redirects替换为正则表达式的组合:

将所有请求从HTTP重定向到HTTPS

$HTTP["scheme"] == "http" {
    # capture vhost name with regex conditiona -> %0 in redirect pattern
    # must be the most inner block to the redirect rule
    $HTTP["host"] =~ ".*" {
        url.redirect = (".*" => "https://%0$0")
        url.redirect-code = 308
    }
}

将特定URL从HTTP重定向到HTTPS

$HTTP["scheme"] == "http" {
    $HTTP["host"] =~ ".*" {
        url.redirect = ("^/phpmyadmin/.*" => "https://%0$0")
        url.redirect-code = 308
    }
}

更新者:gstrauss 1年多前 · 33个修订版本