动态配置的虚拟主机

发布于 2024-10-10 16:44:44 字数 860 浏览 12 评论 0原文

喂! 我想为将来使用 mod_rewrite 创建的所有子域准备一个动态虚拟主机。所有子域的配置方式几乎相同,因此我考虑使用动态 VH 配置。 我希望每个子域的文档根目录为 /home/user/public_html/subdomainName。

我尝试过以下配置,但没有成功:

<VirtualHost xxx.xxx.xxx.xxx:80>
    # get the server name from the Host: header
    UseCanonicalName Off

    <Directory /home/user/public_html/>
    # ExecCGI is needed here because we can't force
    # CGI execution in the way that ScriptAlias does
      Options FollowSymLinks ExecCGI
    </Directory>

    RewriteEngine On

    # a ServerName derived from a Host: header may be any case at all
    RewriteMap lowercase int:tolower

    #rule that is suposed to set document root of virtual host!??? 
    RewriteRule ^([a-z-]+)\.domain\.com/?(.*) /home/user/public_html/$1/$2

</VirtualHost>

规则或某些东西似乎是错误的并且不适用。我以前从未使用过动态 VH,所以我不知道我错在哪里......

Helo!
I would like to prepare a dynamic virtual host for all subdomains that will be created in future using mod_rewrite. All subdomains would be configured prety much same way, so i thought of using dynamic VH configuration.
I want my document root for each subdomain to be /home/user/public_html/subdomainName.

I've tried with following configuration, but had no success:

<VirtualHost xxx.xxx.xxx.xxx:80>
    # get the server name from the Host: header
    UseCanonicalName Off

    <Directory /home/user/public_html/>
    # ExecCGI is needed here because we can't force
    # CGI execution in the way that ScriptAlias does
      Options FollowSymLinks ExecCGI
    </Directory>

    RewriteEngine On

    # a ServerName derived from a Host: header may be any case at all
    RewriteMap lowercase int:tolower

    #rule that is suposed to set document root of virtual host!??? 
    RewriteRule ^([a-z-]+)\.domain\.com/?(.*) /home/user/public_html/$1/$2

</VirtualHost>

The rule or somethinh seems to be wrong andit doesn't apply. I've never worked with dynamic VH before so i have no idea where i'm wrong...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖不如嘢 2024-10-17 16:44:44

这是在您之前很多人都有过的需求。因此,有一个 apache 模块可以为您执行此操作 mod_vhost_alias http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

提供动态配置
海量虚拟主机

This is a need a lot of people had before you. So there's an apache module which can do that for you mod_vhost_alias http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

Provides for dynamically configured
mass virtual hosting

眼眸 2024-10-17 16:44:44

您还可以使用通配符 dns 来代替动态虚拟主机,这意味着将 {anything}.yourdomain.tld 配置为指向您的服务器。

这也将为每个域提供相同的文档根。

请注意,您需要将 ServerAlias *.yourdomain.tld 添加到虚拟主机条目

instead of dynamic virtual hosts, you can also do this using wildcard dns which means configuring {anything}.yourdomain.tld to point to your server.

This will also get you same document root for each domain.

note that you will need to add ServerAlias *.yourdomain.tld to the virtual host entry

你的呼吸 2024-10-17 16:44:44

使用 RewriteRule 您无法访问域名。还对该部分使用 RewriteCond 并使用 %1 等进行反向引用。

RewriteCond %{HTTP_HOST} ^([a-z-]+)\.domain\.com$ [NC]
RewriteRule (.*) %1/$1

唯一的问题是这将创建一个在子域名字符串前面添加的连续循环。所以我通常做的是为子域创建一个单独的文件夹,例如“子域”

RewriteRule ^subdomains - [L]
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.domain\.com$ [NC]
RewriteRule (.*) subdomains/%1/$1

With RewriteRule you can't access the domainname. Also use RewriteCond for that part and use %1 etc. for backreference.

RewriteCond %{HTTP_HOST} ^([a-z-]+)\.domain\.com$ [NC]
RewriteRule (.*) %1/$1

The only problem is this will create a continues loop of prepending the subdomainname string. So what I normally do is create a separate folder for subdomains, like 'subdomains'

RewriteRule ^subdomains - [L]
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.domain\.com$ [NC]
RewriteRule (.*) subdomains/%1/$1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文