0

I have two domain names but I have each domain name directing to the same IP. How could I redirect each domain to the correct place using the index.html file? Below is some pseudo code:

if web adddress= http://siteA.com forward too http://siteA.com/siteA_Dir
if web address = http://SiteB.com forward too http://siteB.com/siteB_Dir
else go to localhost:80

I do not want to use a htaccess file as I have not made one. I currently have this code in my index.html file:

<meta http-equiv="refresh" content="0; url=http://siteA/SiteA_Dir" />
Dylan.evans007
  • 113
  • 2
  • 10
  • Possible duplicate of [.htaccess redirect domain to subdirectory without changing URL](http://stackoverflow.com/questions/20847256/htaccess-redirect-domain-to-subdirectory-without-changing-url) – Panama Jack Feb 08 '16 at 20:10

4 Answers4

1

You can achieve this in javascript:

<script>

var domain = window.location.href;

domain = domain.replace('http://', '');
domain = domain.replace('.com/', '');
domain = domain.replace('.com', '');

switch (domain) {
    case 'siteA': window.location.href = 'http://siteA.com/siteA_Dir'; break;
    case 'siteB': window.location.href = 'http://siteB.com/siteB_Dir'; break;
    default: window.location.href = 'http://127.0.0.1/';
}

</script>
Rounin
  • 27,134
  • 9
  • 83
  • 108
0

It's not possible to have this logic in pure HTML, though a small javascript snippet should suffice

<script>
  (function(loc) {
    if (loc.indexOf('http://siteA.com/') === 0) {
        window.location = 'http://siteA.com/siteA_dir';
    }
    else if (loc.indexOf('http://siteB.com/') === 0) {
        window.location = 'http://siteB.com/siteB_dir';
    }
    else {
        window.location = 'http://localhost:80';
    }
  })(window.location);
</script>
Rogier Spieker
  • 4,087
  • 2
  • 22
  • 25
  • What if the URL started with "www."? – kzhao14 Feb 08 '16 at 20:56
  • then it can be easily added to the list. the code merely shows a solution, if OP wanted a clever solution in client javascript capable of doing all kinds of nifty redirects, it would have said so in the question. None of us included any request parameters and/or anchor preservation either as this wasn't part of the question. – Rogier Spieker Feb 08 '16 at 21:22
0

You can use JS:

    var currUrl = window.location.href;
    var urlbase = currUrl.replace("https://www.","");
    var urlbase = urlbase.replace("http://www.","");
    var urlbase = urlbase.replace("https://","");
    var urlbase = urlbase.replace("http://","");
    var urlbase = urlbase.substr(0,9);

    switch(urlbase) {
      case "siteA.com":
        document.write("site A: " + urlbase);
        break;
      case "siteB.com":
        document.write("site B: " + urlbase);
        break;
      default:
        document.write("N/A: " + urlbase);
        break;
    }
kzhao14
  • 2,470
  • 14
  • 21
  • I can see why you asked "how about urls starting with 'www'?", as you cleverly thought of that. Now let me ask you some questions: how does your code redirect? Why do you redeclare the same `urlbase` var over and over again? And finally, why didn't you use a single `.replace(/^https?:\/\/(?:www\.)?/, '')`? I'm not looking for answers to these questions, it just struck me as odd that you asked the same question on two answers, which - to me - seemed you wanted to point out your cleverness, so I thought to return the favour ;-) – Rogier Spieker Feb 08 '16 at 21:19
0

Rename index.html" to "index.php"

Add this code to your file

<?php 
$CurrentUrl='http://'. $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
  if ($CurrentUrl=="http://siteA.com")
  echo "<META HTTP-EQUIV ='Refresh' Content ='0; URL =http://siteA /SiteA_Dir'>";

   elseif ($CurrentUrl=="http://siteB.com")
     echo "<META HTTP-EQUIV ='Refresh' Content ='0; URL =http://siteB/SiteB_Dir'>";

  else
     echo "<META HTTP-EQUIV ='Refresh' Content ='0; URL =http://localhost'>";

?>
FarZan Takhsha
  • 154
  • 1
  • 1
  • 11