How can I refer to a relative subdomain in HTML?

后端 未结 2 873
灰色年华
灰色年华 2020-12-11 15:49

Let\'s say I have a website which is accessed by multiple domains, e.g. domain1.com and domain2.com.

If I have a relative link, such as

相关标签:
2条回答
  • 2020-12-11 16:22

    It is not possible with relative paths, because subdomain is in fact a whole different domain.

    If you really can't use absolute URL, but can use PHP, you could try this proxy script:

    <?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);
    

    Save it into a file, eg. imgproxy.php, and then you can link images on the other subdomain like this:

    <img src="imgproxy.php?url=images/logo.png">
    
    0 讨论(0)
  • 2020-12-11 16:27

    No. You'll have to give the whole domain. To link from domain1.com to wiki.domain1.com, the link has to look like href="http://wiki.domain1.com".

    0 讨论(0)
提交回复
热议问题