3

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> 
deadalic
  • 35
  • 3
  • Maybe getting the addressed domain (like shown here: https://stackoverflow.com/questions/11401897/get-the-current-domain-name-with-javascript-not-the-path-etc)& storing it as variable, you'lle be able to print an html code like this one: `` – xKobalt Mar 05 '20 at 09:11
  • Thanks, but for some domains the name is different from the subdomain used by the provider. For example graphic-design.com gets the subdomain graphicdesign.domain.tld, because the provider does not support special characters in his subdomains... – deadalic Mar 05 '20 at 09:38

1 Answers1

1

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.

Lucas Duval
  • 188
  • 1
  • 7
  • Cool, that helps (y) I still have a few specific cases where there is another domain that should also link to the same page, e.g. graphic-design.com and shades-design.com to graphicdesign.domain.tld. I would maintain the links manually, though. (y) – deadalic Mar 05 '20 at 10:58
  • Yes, for these special case, I think a database with all these keydomain => subdomain would be useful. Don't forget to mark answer as accepted if your problem is solved. – Lucas Duval Mar 05 '20 at 11:03
  • Apparently the src parameter in the script is not set correctly; I get the error `Uncaught TypeError: Cannot set property 'src' of null`. Ideas? If I execute the corresponding line of the script in the console, it works without problems. – deadalic Mar 05 '20 at 11:31
  • Have you added the id="website" on your iframe tag ? – Lucas Duval Mar 05 '20 at 12:07
  • This is the current code: ` ... ` – deadalic Mar 05 '20 at 12:38
  • It's seems that document.getElementById("website") can't find a element with "website" as an id. Can you put the whole code on a [pen.io](https://codepen.io/) and then share it here – Lucas Duval Mar 05 '20 at 13:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209089/discussion-between-deadalic-and-lucas-duval). – deadalic Mar 05 '20 at 13:53