0

考虑我的域是 www.example.com

我如果在 php/html 文件中使用像/images/sample.jpg这样的路径,它实际上是指www.example.com/images/sample.jpg

同样,如果我对www.subdomain.example.com做同样的事情

/images/sample.jpg --> 它指的是 www。子域.example.com/images/sample.jpg

但我想从主域引用图像,而不在子域网页中提供完整的网址。

我的主要域文件是 ketp 在:root/www/

我的子域文件保存在:root/public_html/admin/

稍后我可能会更改网址.. 所以我不想在我的代码中硬编码完整的网址。

4

4 回答 4

1

你只想使用html吗?如果您使用 JavaScript,您可以通过编程方式创建子域。您不想指定完整网址的原因是什么?

编辑:

一种方法是使用 javascript。

如果在文本开头为域设置变量:

<html>
<head>
<script>
var webaddr = "www.example.com";
</script>
</head>

然后在您想编写实际链接时使用此代码:

<body>
<img src="<script>document.write(webaddr)</script>/images/sample.jpg">
</body>
</html>

然后您可以在移动域时更改变量中的域。那行得通吗?

于 2013-09-24T18:18:38.630 回答
1

此代码将检测 URL 并添加指定的子域:

      <script>
            var subdomain = "subdomain"; //change this variable to match your subdomain

            var webaddr = (document.URL); //This gets the current URL
            var newURL = (webaddr.substring(0, 7) + (subdomain) + "." + (webaddr.substring(7))); //This creates the new URL with the subdomain added
        </script>

    Old link:
        <a href="<script>document.write(webaddr)</script>/images/sample.jpg">"<script>document.write(webaddr)</script>/images/sample.jpg"</a>

        <br>
New link:
        <a href="<script>document.write(newURL)</script>/images/sample.jpg">"<script>document.write(newURL)</script>/images/sample.jpg"</a>

http://jsfiddle.net/wFMzx/2/

于 2013-09-24T19:15:27.173 回答
1

如果可以使用 PHP 或 ASP,则可以使用变量(或常量)作为要使用的主机名的占位符。在全局配置文件中设置该变量,以便在任何地方都可以访问它。

这是一个使用常量的 PHP 示例:

<?php
define( 'HOST', 'www.example.com' );
?>

然后像这样使用它:

<img src="<?php echo HOST ;?>/images/sample.jpg">

然后,您可以随时更改首选主机名,您所要做的就是更新常量。

于 2013-09-24T18:47:19.473 回答
0

看起来您想要类似于 apache mod_rewrite 所做的事情,即将一种类型的 url 重定向到另一种

例如,当您提供类似

/maindomain/images/someimage.jpg 

这将被视为

www.subdomain.example.com/maindomain/images/someimage.jpg

将其重定向到

www.example.com/images/someimage.jpg

Micronovae似乎有一个名为 mod_rewrite 的 mod,它看起来像 apache mod_rewrite

然后你可以在你的 www.subdomain.example.com 网站上使用类似下面的东西

RewriteRule ^/maindomain/images/(.*)$ http://www\.example\.com/images/$1 [R=301,L]

你必须购买的 Micronovae 模组,你可以尝试使用http://www.iis.net/downloads/microsoft/url-rewrite但它可能有不同的语法来进行重写

于 2013-09-24T18:30:55.880 回答