虚拟主机映射到目录结构

发布于 2024-12-26 13:00:53 字数 709 浏览 2 评论 0原文

我已经阅读了 apache 上的大量虚拟主机帮助,但作为一个相对新手,我有点困惑。我知道我的问题必须是可以实现的,因为这是一个非常基本的问题,但我迷失了...

基本上我有很多虚拟主机指向我的服务器,并且它们指向其 DocumentRoot 的文件结构是一致的...例如。

www.mydomain.com -> /home/blah/vhosts/mydomain.com/www/public
abc.mydomain.com -> /home/blah/vhosts/mydomain.com/abc/public

www.another.co.uk -> /home/blah/vhosts/another.co.uk/www/public
def.another.co.uk -> /home/blah/vhosts/another.co.uk/def/public

如果可能,我还需要将非 www.* 重定向到 www.*,但要考虑到子域的可能性,以便:

  • mydomain.com 重定向到 www.mydomain。 com
  • abc.mydomain.com NOT 按原样重定向到 www.mydomain.com 使用上面的定义处理(由于目录结构)

这可能吗?

I have read the mass virtual host help on apache but as a relative newbie I am left a little confused. I know my problem must be acheiveable as it is a pretty basic problem but i am lost...

Basically I have lots of virtual hosts pointing to my server and the file structure that they point to for their DocumentRoot is consistent... e.g..

www.mydomain.com -> /home/blah/vhosts/mydomain.com/www/public
abc.mydomain.com -> /home/blah/vhosts/mydomain.com/abc/public

www.another.co.uk -> /home/blah/vhosts/another.co.uk/www/public
def.another.co.uk -> /home/blah/vhosts/another.co.uk/def/public

If possible, I also need to redirect non-www.* to www.* but taking into account the possibility of a subdomain, so that:

  • mydomain.com is redirected to www.mydomain.com
  • abc.mydomain.com is NOT redirected to www.mydomain.com as it is
    handled with the definition above (due to directory structure)

Is this at all possible?

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

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

发布评论

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

评论(1

秋意浓 2025-01-02 13:00:53

这是我对所有虚拟主机所做的操作:我正在使用 Apache writemap。

与合作伙伴创建一个新的“partner.txt”文件,如下所示:

0 www
1 partner1
2 partner2
3 partner1

然后编译它,并将其添加到重写规则中以查明前缀是否是合作伙伴,如下所示:

<VirtualHost *>
    ServerAdmin [email protected]
    DocumentRoot "/web/htdocs/olivier/mydomain.fr/dev/website"
    ServerName mydomain.fr
    ServerAlias *.mydomain.fr
    ErrorLog "/web/logs/mydomain.error.log"
    CustomLog "|/opt/httpd/bin/rotatelogs /web/logs/mydomain.fr/access_log.%Y-%m-%d-%H_%M_%S.log 5M" combined
    ErrorDocument 404 /404.php

    RewriteEngine On
    # trying to hack = redirect:
    RewriteRule (.*)setup.php http://disneyland.fr/ [NC,R,L]
    RewriteRule (.*)admin(.*) http://disneyland.fr/ [NC,R,L]

    # if your host doesn't begin with "www" add it and redirect:
    RewriteCond %{HTTP_HOST} ^mydomain\.(fr|com|net|org|eu) [NC]
    RewriteRule (.*) http://www.mydomain.%1$1 [QSA,R=301,L]

    RewriteMap partners \
      dbm:/web/htdocs/olivier/mydomain.fr/rewriterules/partners.map

    # test if known partner:
    RewriteCond %{HTTP_HOST} (([a-zA-Z0-9\-]+)\.)mydomain.com$
    RewriteRule (.*) - [QSA,E=PARTNER:${templates:%1|notfound}]

    # if partner not found or empty, 404:
    RewriteCond %{ENV:PARTNER} ^$ [OR]
    RewriteCond %{ENV:PARTNER} notfound
    RewriteRule .* - [R=404,L]
</VirtualHost>

Here what I do for all my vhosts: I'm using Apache writemap.

Create a new "partner.txt" file with partners like this:

0 www
1 partner1
2 partner2
3 partner1

Then compile it, and add use it into your rewriterules to find out whether the prefix is a partner or not, like this:

<VirtualHost *>
    ServerAdmin [email protected]
    DocumentRoot "/web/htdocs/olivier/mydomain.fr/dev/website"
    ServerName mydomain.fr
    ServerAlias *.mydomain.fr
    ErrorLog "/web/logs/mydomain.error.log"
    CustomLog "|/opt/httpd/bin/rotatelogs /web/logs/mydomain.fr/access_log.%Y-%m-%d-%H_%M_%S.log 5M" combined
    ErrorDocument 404 /404.php

    RewriteEngine On
    # trying to hack = redirect:
    RewriteRule (.*)setup.php http://disneyland.fr/ [NC,R,L]
    RewriteRule (.*)admin(.*) http://disneyland.fr/ [NC,R,L]

    # if your host doesn't begin with "www" add it and redirect:
    RewriteCond %{HTTP_HOST} ^mydomain\.(fr|com|net|org|eu) [NC]
    RewriteRule (.*) http://www.mydomain.%1$1 [QSA,R=301,L]

    RewriteMap partners \
      dbm:/web/htdocs/olivier/mydomain.fr/rewriterules/partners.map

    # test if known partner:
    RewriteCond %{HTTP_HOST} (([a-zA-Z0-9\-]+)\.)mydomain.com$
    RewriteRule (.*) - [QSA,E=PARTNER:${templates:%1|notfound}]

    # if partner not found or empty, 404:
    RewriteCond %{ENV:PARTNER} ^$ [OR]
    RewriteCond %{ENV:PARTNER} notfound
    RewriteRule .* - [R=404,L]
</VirtualHost>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文