【问题标题】:Referring css and js files from sub domain从子域引用 css 和 js 文件
【发布时间】:2014-09-06 16:37:10
【问题描述】:

我有一个网站,其主题使用它自己的样式表和 javascript。

我想创建多个子域,使用与主站点不同的主题。

当我创建了一个子域并引用子域的 css 和 js 文件时,它无法找到它

我希望子域引用与主站点主题不同的主题

我的主站点位于 public_html/示例
public_html/example/css
public_html/example/css/exm.css

我的子域具有以下层次结构
public_html/example/subdomain/sub1
public_html/example/subdomain/sub2

而且css和js都在
public_html/example/子域
public_html/example/subdomain/css
public_html/example/subdomain/css/exm.css

public_html/example/subdomain/sub1/index.html

所以我想从 sub/index.html 引用 index.html 中的 exm.css

【问题讨论】:

  • 请贴一些代码?

标签: javascript php html css


【解决方案1】:

如果您对所有页面都有相同的index.php,但只想根据当前子域包含不同的CSS 和Javascript 文件,您可以使用HTTP_HOST 提取它,然后包含相应的文件。您可以将它们存储在 your.domain.com/sub/js/theme.jsyour.domain.com/sub/css/theme.css

PHP:

$includePath = "/";
// array(0 => "sub", 1 => "example", 2 => "com");
$domainParts = explode(".", $_SERVER['HTTP_HOST']);
$domainPartsCount = count($domainParts);
if ($domainPartsCount >= 3)
    $includePath .= $domainParts[$domainPartsCount - 3] . "/";

HTML:

<head>
    <link rel="stylesheet" type="text/css" href="<?= $includePath ?>css/theme.css" />
    <script type="text/javascript" src="<?= $includePath ?>js/theme.js"></script>
</head>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多