如何将 Lighttpd simple_vhost 与 www 结合起来以实现非 www 重定向?

如何将 Lighttpd simple_vhost 与 www 结合起来以实现非 www 重定向?

我已经使用 Lighttpd simple_vhost 模块设置了一个简单的虚拟主机,并希望从 www 到非 www 进行永久 301 重定向。

以下是我的配置文件中最重要的部分:

server.modules = (
  "mod_simple_vhost",
  "mod_redirect",
)

simple-vhost.server-root = "/Websites/"

$HTTP["host"] =~ "^www\.(.*)$" {
  url.redirect  = (
    "^/(.*)" => "http://%1/$1",
  )
}

重定向块取自 Lighttpd 文档,但根本不起作用。simple_vhost 模块文档中提到了一个微妙的困难:

您必须记住,条件和简单虚拟主机会互相干扰。

我已经尝试过那里提出的解决方案(http://redmine.lighttpd.net/projects/1/wiki/Docs_ModSimpleVhost),但仍然没有成功:

$HTTP["host"] != "www.example.org" {
    simple-vhost.server-root = "/Websites" 
    simple-vhost.default-host = "example.org"  
}

$HTTP["host"] == "www.example.org" {
    server.document-root = "/Websites/example.org" 
}

使用虚拟主机时,从 www 重定向到非 www 的正确方法是什么?

这是我的完整配置文件。

server.modules = (
"mod_access",
"mod_simple_vhost",
"mod_alias",
"mod_accesslog",
"mod_compress",
"mod_redirect",
"mod_cgi"
)

server.follow-symlink = "enable"

server.document-root = "/Websites"

server.errorlog = "/Websites/error.log"
server.breakagelog = "/Websites/breakage.log"

index-file.names = ("index.html")

accesslog.filename = "/Websites/access.log"

## deny access the file-extensions
#
# ~ is for backupfiles from vi, emacs, joe, ...
# .inc is often used for code includes which should in general not be part
# of the document-root
url.access-deny = ( "~", ".inc" )

##
# which extensions should not be handle via static-file transfer
#
# .php, .pl, .fcgi are most often handled by mod_fastcgi or mod_cgi
static-file.exclude-extensions = ( ".php", ".pl")

server.port = 80
server.bind = "10.20.30.40"

## to help the rc.scripts
server.pid-file = "/var/run/lighttpd.pid"

## virtual directory listings
dir-listing.encoding = "utf-8"
server.dir-listing = "enable"

server.username = "lighttpd"
server.groupname = "lighttpd"

#### compress module
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ("text/plain", "text/html", "application/x-javascript", "text/css")

#### external configuration files
## mimetype mapping
include_shell "/usr/share/lighttpd/create-mime.assign.pl"

## load enabled configuration files,
## read /etc/lighttpd/conf-available/README first
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

# Simple virtual host.
$HTTP["host"] != "www.example.com" {
simple-vhost.server-root = "/Websites/"
}

$HTTP["host"] == "www.example.com" {
server.document-root = "/Websites/example.com/"
}

答案1

simple-vhost.default-host = "example.org"

将负责 www.example.org 子域(除非它的文件夹存在)。

这两个配置片段看起来都不错;但你没有粘贴完整的配置,缺少包含内容。MIME 类型可能并不重要,但是

include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

可以做很多事情。

lighttpd -p -f /etc/lighttpd/lighttpd.conf

显示完整配置。

此外,正如评论所示,告诉我们您的测试情况(“根本不起作用”是什么意思?)。使用 curl 进行测试以确保您的浏览器缓存不是问题所在。

看一下http://redmine.lighttpd.net/projects/lighttpd/wiki/DebugVariables也。

答案2

此步骤适用于 alpine linux

加载优质 laravel、slim 和 codeigniter

uncomment:       "mod_rewrite",
uncomment:       "mod_simple_vhost",

var.basedir  = "/var/www/localhost"
server.document-root = var.basedir + "/htdocs"

接下来的三行创造了奇迹!

simple-vhost.server-root = "/var/www/localhost/htdocs/vhost"
simple-vhost.default-host = "myhost1.com"
simple-vhost.document-root = "/public"

配置重写

url.rewrite-if-not-file = ("(.*)" => "/index.php/$0")

为每个主机创建一个文件夹,例如:

myhost1.com    /var/www/localhost/htdocs/vhost/myhost1.com
otherhost.com    /var/www/localhost/htdocs/vhost/otherhost.com

将您的主机添加到 /etc/hosts 文件

127.0.0.1  myhost1.com
127.0.0.1  otherhost.com

重新加载 lighttpd

rc-service -vv lighttpd restart

相关内容