如何在HTML中引用相对子域?

时间:2012-05-29 04:31:38

标签: html hyperlink subdomain

假设我有一个由多个域访问的网站,例如domain1.comdomain2.com

如果我有相对链接,例如href="/wiki",那么无论我访问哪个域名,该链接都会将我带到正确的位置。

让我们说我想使用wiki.domain1.comwiki.domain2.com,我是否可以通过某种方式建立相对于域名的链接?

如果没有,当多个域指向同一服务器时,是否有一种优雅的方式来处理上面的wiki链接?

2 个答案:

答案 0 :(得分:9)

没有。你必须给整个域名。要从domain1.com链接到wiki.domain1.com,该链接必须看起来像href="http://wiki.domain1.com"

答案 1 :(得分:5)

相对路径是不可能的,因为子域事实上是一个完全不同的域。

如果你真的不能使用绝对URL,但可以使用PHP,你可以试试这个代理脚本:

<?php

if(!isset($_GET['url'])) {
    die('Missing URL!');
}

$subdomain_url = 'http://subdomain.example.com/';
$file_path = $_GET['url'];

$file_url = $subdomain_url . $file_path;

$mime = finfo_open(FILEINFO_MIME, $file_url);


header('Content-Type: ' . $mime);
header('Content-Transfer-Encoding: Binary'); 
header('Content-disposition: inline; filename="' . basename($file_path) . '"'); 

readfile($file_url);

将其保存到文件中,例如。 imgproxy.php ,然后您可以像这样链接其他子域上的图像:

<img src="imgproxy.php?url=images/logo.png">
相关问题