【问题标题】:Subdomains using same back-end with different CSS使用具有不同 CSS 的相同后端的子域
【发布时间】:2014-09-22 23:36:49
【问题描述】:

我为我的网站创建了多个子域...

主站点:
例子.com

子域:
name1.example.com
name2.example.com
name3.example.com
name4.example.com

子域将使用与主站点相同的 php 和 html 后端。唯一的区别是 CSS。

我正在考虑是否可能只需要在每个子域的文档中包含我需要的文件,以便我可以快速轻松地进行全面更新。

我不确定这是处理此问题的最佳方法,但一遍又一遍地完全克隆网站似乎是多余的。有没有更好的方法来处理这个问题?

【问题讨论】:

  • 只需在每个子域<link rel="stylesheet" type="text/css" href="//example.com/css/global_styles.css">上为您的 CSS 文件使用绝对路径@

标签: php html css .htaccess


【解决方案1】:

有更好的方法,是的。这就是我处理这种情况的方式。

假设你的前端有这个文件树;

- /
 - [all-backend related files]
 - sites/
 |- default/
   |- pages/
     |- index/
       | - index.css
 |- name1/
   |- pages/
     |- index/
       | - index.css
 |- name2/
   | - pages/
      | - index/
        | - index.css

你的 PHP 看起来会像这样;

function isSubdomain() {
// http://stackoverflow.com/a/15852997/3000179
    $host = $_SERVER['HTTP_HOST'];
    $host = explode('.',$host);
    $host = (is_array($host)?$host[0]:$host);
    return $host;
}

//Assume we are on the site1.domain.com/index
//Assume we are on your template file, and we're at the
//<head> tags within your page
if( file_exists(__DIR__ .'/'. isSubdomain() .'/pages/index/index.css') ) {
   echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"".  __DIR__ ."/". isSubdomain() ."/pages/index/index.css\">";
} else {
   echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"".  __DIR__ ."/default/pages/index/index.css\">";
}

然后,将您的所有子域都指向DocumentRoot/,然后让PHP 加载相关的CSS 文件。

【讨论】:

  • 很高兴听到@Urbo425!
【解决方案2】:

我有 java 背景,但我确信 php 中有类似的东西,您可以将每个子域的所有 css 文件放在单独的文件中。然后在您的 php 代码中,检查请求的 url 并包含包含该子域的 css 的文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 2015-08-29
    • 1970-01-01
    • 2013-10-05
    相关资源
    最近更新 更多