Apache 头痛:虚拟主机配置

Apache 头痛:虚拟主机配置

我正在尝试实现以下结果:

我有多个指向同一个 documentroot 的域,其中包含所有域上正在使用的文件(index.php、css 文件等):

目录:/cms/

然后我有几个域:

  • www.domain1.com/*
  • fr.domain1.com/*

都应该指向 D:/files/domain1.com/*

  • www.domain2.com/*
  • de.domain2.com/*

都应该指向 D:/files/domain2.com/*

然而

  • www.domain1.com/adminstyle.css
  • fr.domain1.com/adminstyle.css
  • www.domain2.com/adminstyle.css
  • de.domain2.com/adminstyle.css

应全部指向 C:/cms/adminstyle.css(如果在“每个域”目录中找不到文件,则应返回到“常规”目录)

使用 apache 可以实现吗?我一直在尝试虚拟主机/目录和别名,但还没有成功。

答案1

你可以做类似的事情:

NameVirtualHost 1.2.3.4:80
Alias /shared D:/files/shared

<VirtualHost 1.2.3.4:80>
ServerName domain1.com
ServerAlias www.domain1.com *.domain1.com
DocumentRoot D:/files/domain1.com
</VirtualHost>

<VirtualHost 1.2.3.4:80>
ServerName domain2.com
ServerAlias www.domain2.com *.domain2.com
DocumentRoot D:/files/domain2.com
</VirtualHost>

将您的常用文件放在 D:/files/shared 中,并以 img src="/shared/whatever" 的形式引用它们

答案2

这应该可以。

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/adminstyle\.css$
RewriteCond %{DOCUMENT_ROOT}/adminstyle.css !-f
RewriteRule ^(.+) C:/cms/adminstyle.css [L]
RewriteRule ^(.+) - [PT]

看:http://httpd.apache.org/docs/2.2/rewrite/rewrite_guide.html#multipledirs

答案3

<VirtualHost *:80>
    DocumentRoot c:/cms/
    ServerName shared.domain1.com
    ServerAlias shared.domain2.com
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot c:/files/domain1.com/
    #Yes, taking liberty with the name of your domain(s) here.  Correct it if my assumptions are wrong.
    ServerName domain1.com
    ServerAlias *.domain1.com

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) http://shared.domain1.com/$1 [L,QSA]
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot c:/files/domain2.com/
    #Yes, taking liberty with the name of your domain(s) here.  Correct it if my assumptions are wrong.
    ServerName domain2.com
    ServerAlias *.domain2.com

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) http://shared.domain2.com/$1 [L,QSA]
</VirtualHost>

相关内容