【问题标题】:Using js and css of primary domain from subdomain从子域使用主域的 js 和 css
【发布时间】:2014-11-08 20:44:01
【问题描述】:

假设我有一个名为 example.com 的域,现在我正在使用子域 mobile.example.com 创建我的移动网站。

mobile.example.com 显示http://example.com/mobile 目录的内容。

example.com 目录结构是

-- css
-- images
-- js |--- custom.js
-- mobile |--- start.html
-- html files

现在我想在 start.html 文件中使用 custom.js,即 mobile.example.com/start.html

我做不到

<script type="text/javascript" src="/js/custom.js"></script>

在 start.html 上,因为它表示 mobile.example.com/js/custom.js,不可用。

还有

<script type="text/javascript" src="../js/custom.js"></script>

不起作用,因为它也意味着http://mobile.example.com/../js/custom.js

我也做不到

<script type="text/javascript" src="http://example.com/js/custom.js"></script>

因为除了 example.com 我还有另一个域进行测试。我不想在上传到测试服务器和产品服务器时更改我的代码。

我也不能将 js/custom.js 复制到 example.com/mobile 文件夹,因为它会带来冗余。

任何其他想法如何将我的 js 和 css 请求重定向到主域?

【问题讨论】:

    标签: javascript html css .htaccess subdomain


    【解决方案1】:

    如果您无法从原始路径复制内容,请创建符号链接。从mobile/ 目录执行此操作:

    ln -s ../js .
    ln -s ../css .
    ln -s ../images .
    

    然后将它们用作:

    <script type="text/javascript" src="/js/custom.js"></script>
    

    【讨论】:

    【解决方案2】:

    如果你使用 PHP,你可以使用 kinda 代理:

    /pxy.php?path=/js/custom.js
    

    pxy.php,你可以添加这个:

    <?php
        die (file_get_contents("http://example.com" . $_GET["path"]));
    ?>
    

    【讨论】:

    • 对不起,我的项目中没有使用 php
    • 但如果您想使用此解决方案,您可以使用另一个服务器端代码。不错的主意
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 2020-10-04
    相关资源
    最近更新 更多