如何根据 URL 更改文档根目录

Sup*_*tik 2 linux apache-2.2

我在 RHEL 5.3 中使用 httpd-2.2.3-22。

如何根据 URL 重定向到 Linux 中的不同文档根目录。

例子:

testdomain.com 的文档根目录是 /var/www/vhost/testdomain.com/httpdocs

如果 URL 是http://web1.testdomain.com那么它应该将文档根指向 /var/www/vhost/testdomain.com/httpdocs/web1。

如果 URL 是http://web2.testdomain.com那么它应该将文档根指向 /var/www/vhost/testdomain.com/httpdocs/web2。

其中 web1 和 web2 是父文档根目录下的两个不同文件夹。

请让我知道在这种情况下如何配置apache?

温暖的问候

苏普拉蒂克

小智 5

听起来您正在寻找动态虚拟主机。Apache 文档中有一篇关于此的非常好的文章:

动态配置的海量虚拟主机

有多种方法可以做到这一点,但最灵活的可能是 mod_rewrite。根据您的示例,您可以使用以下虚拟主机配置:

<VirtualHost *>
        ServerName testdomain.com
        ServerAlias *.testdomain.com

        RewriteEngine on

        RewriteMap lowercase int:tolower

        # check the hostname is right so that the RewriteRule works
        RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-z0-9-]+\.testdomain\.com$

        # concatenate the virtual host name onto the start of the URI
        # the [C] means do the next rewrite on the result of this one
        RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]

        # now create the real file name
        RewriteRule ^([a-z0-9-]+)\.testdomain\.com/(.*) /var/www/vhost/testdomain.com/httpdocs/$1/$2
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)