user3780621 February 2016

how to I redirect a domain using the index.html file?

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" />

Answers


Rogier Spieker February 2016

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>


Rounin February 2016

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>


k97513 February 2016

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;
    }


FarZan Takhsha February 2016

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'>";

?>

Post Status

Asked in February 2016
Viewed 2,118 times
Voted 8
Answered 4 times

Search




Leave an answer