Can an iFrame be integrated depending on the addressed domain?

Viewed 388

first of all I would like to mention that I am usually not a developer. Please have a little indulgence ;-)

I maintain websites of several customers, which are hosted by a provider. However, you only get a sub-domain from the provider, which is then also shown in the address bar. In order to display the own domain, I thought about redirecting the domains to my own server and integrate the external website via iFrame.

However, my server is not a primarily webserver and is only designed to host one single website. Therefore the web server always answers with its default index.html.

Do you have an idea how I can include a corresponding iFrame in this file depending on the called domain and if possible also change the title in the head? Thank you very much in advance for your help!

The index.html currently looks like this:

<html>

<head>
      <title>Web Client</title>
</head>

<style>
body {
      margin: 0;
      padding: 0;
}
body, iframe {
      width: 100%;
      height: 100%;
}
iframe {
      border: 0;
}
</style>

<body>
      <iframe src="https://sub.domain.tld/"/>
</body>

</html> 
1 Answers

You can try something like that using javascript :

<html>

<head>
    <title>Web Client</title>
</head>

<style>
    body {
        margin: 0;
        padding: 0;
    }
    body, iframe {
        width: 100%;
        height: 100%;
    }
    iframe {
        border: 0;
    }
</style>

<body>
    <iframe id="website" src=""></iframe>
</body>
<script type="text/javascript">
    //Get the current URL
    var currentURL = window.location.href;
    //Get the subdomain from URL 
    var hostnameURL = currentURL.replace(/(http[s]*\:\/\/)/gi, '').split("/")[0];
    //Set the page title
    document.title = hostnameURL;
    //Transform subdomain to alphaNumeric
    var alphaNumericSubDomainName = hostnameURL.replace(/[^\w]/gi, '');
    //Set the subdomain to the iframe (don't forget to change the domain name below)
    document.getElementById("website").src = "https://"+ alphaNumericSubDomainName + ".domain.tld";
</script>
</html> 

I first get the current URL. Then I get the hostname (domain without TLD) of the URL.

I use this hostname to set the title. Then i remove special characters in the hostname and concat this sanitized hostname as a subdomain.

Finally i put this new URL as the source URL for the Iframe.

Related