简体   繁体   English

Apache 通过 Virtualhost 区分域

[英]Apache Differentiate domains by Virtualhost

I need to serve multiple domains from one folder with minor branding/reskinning changes.我需要从一个文件夹中提供多个域,并进行较小的品牌/重新换肤更改。 To prevent having to maintain a dozen duplicate codebases, I wanted to set an environment variable for each.为了避免维护十几个重复的代码库,我想为每个代码库设置一个环境变量。 I need a secure way that cannot be tampered with "in browser", so I was trying to set something prior to serving the code out.我需要一种不能被“在浏览器中”篡改的安全方式,所以我试图在提供代码之前设置一些东西。

I have the sites setup on my server and I was planning to use SetEnv SITE_CODE for each to determine which site is being served and my PHP code would use get getenv('SITE_CODE');我在我的服务器上设置了站点,我打算为每个站点使用SetEnv SITE_CODE来确定正在服务的站点,并且我的 PHP 代码将使用 get getenv('SITE_CODE'); to determine which skin to show.以确定要显示的皮肤。

However it seems this won't work as I get the last site code for all sites.但是,这似乎不起作用,因为我获得了所有站点的最后一个站点代码。 Is there something I am doing wrong or a better way to do this?有什么我做错了还是有更好的方法来做到这一点?

<VirtualHost *:80>
    ServerAdmin info@site1.com
    ServerName site1.com
    ServerAlias site1.com www.site1.com
    DocumentRoot /var/www/mainsite/
    ErrorLog /var/www/site1.com/logs/error.log
    CustomLog /var/www/site1.com/logs/access.log combined
</VirtualHost>

SetEnv SITE_CODE site1

and

<VirtualHost *:80>
    ServerAdmin info@site2.com
    ServerName site2.com
    ServerAlias site2.com www.site2.com
    DocumentRoot /var/www/mainsite/
    ErrorLog /var/www/site2.com/logs/error.log
    CustomLog /var/www/site2.com/logs/access.log combined
</VirtualHost>

SetEnv SITE_CODE site2

An environment variable set for Apache will affect the Apache user's environment, not a specific end user session, which is why it can't be used reliably for your purpose.为 Apache 设置的环境变量将影响 Apache 用户的环境,而不是特定的最终用户 session,这就是为什么它不能可靠地用于您的目的。

If it were me I'd do one of two things:如果是我,我会做以下两件事之一:

  1. Check the requested domain directly by examining $_SERVER['SERVER_NAME']通过检查$_SERVER['SERVER_NAME']直接检查请求的域
  2. Examine that value then set a session variable at the first request (that is, if that variable is not yet set for the current session).检查该值,然后在第一次请求时设置 session 变量(即,如果尚未为当前会话设置该变量)。 This would not be able to be directly tampered with, if that's important for some reason.如果出于某种原因这很重要,这将无法直接被篡改。

You can basically leave the VirtualHostConfig as it is and use SetEnvIf in your .htaccess file in your webroot folder.您基本上可以保留 VirtualHostConfig 并在您的 webroot 文件夹中的 .htaccess 文件中使用 SetEnvIf。

SetEnvIf Host "my.site1.com" SITE_CODE=Site1

... ...

SetEnvIf Host "my.site2.com" SITE_CODE=Site2 and so on. SetEnvIf Host "my.site2.com" SITE_CODE=Site2等等。

A more in-depth manual on the respective Apache directives and used modules can be found here: https://httpd.apache.org/docs/current/mod/mod_setenvif.html A more in-depth manual on the respective Apache directives and used modules can be found here: https://httpd.apache.org/docs/current/mod/mod_setenvif.html

And as stated in this answer: https://stackoverflow.com/a/4139899/8103283 if for example the module mod_setenvif is not available for you, mod_rewrite can also do the trick.正如这个答案中所述: https://stackoverflow.com/a/4139899/8103283例如,如果模块 mod_setenvif 对您不可用, mod_rewrite 也可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM