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

Ale*_*lex 10 html subdomain hyperlink

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

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

让我们说,我想使用wiki.domain1.comwiki.domain2.com,有什么方法可以链接到这个相对于域名的链接?

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

dec*_*eze 9

不,你必须给整个域名.要链接domain1.comwiki.domain1.com,链接必须看起来像href="http://wiki.domain1.com".


Mig*_*ork 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);
Run Code Online (Sandbox Code Playgroud)

将其保存到文件中,例如。imgproxy.php,然后您可以在另一个子域上链接图像,如下所示:

<img src="imgproxy.php?url=images/logo.png">
Run Code Online (Sandbox Code Playgroud)