【问题标题】:how to I redirect a domain using the index.html file? [closed]如何使用 index.html 文件重定向域? [关闭]
【发布时间】:2016-05-18 15:03:50
【问题描述】:

我有两个域名,但每个域名都指向同一个 IP。如何使用 index.html 文件将每个域重定向到正确的位置?下面是一些伪代码:

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

我不想使用 htaccess 文件,因为我还没有制作。我目前在我的 index.html 文件中有这段代码:

<meta http-equiv="refresh" content="0; url=http://siteA/SiteA_Dir" />

【问题讨论】:

标签: html indexing


【解决方案1】:

将 index.html" 重命名为 "index.php"

将此代码添加到您的文件中

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

?>

【讨论】:

    【解决方案2】:

    你可以使用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;
        }

    【讨论】:

    • 我明白你为什么问“以'www'开头的网址怎么样?”,正如你聪明地想到的那样。现在让我问你一些问题:你的代码是如何重定向的?为什么你一遍又一遍地重新声明相同的urlbase var?最后,您为什么不使用单个.replace(/^https?:\/\/(?:www\.)?/, '')?我不是在寻找这些问题的答案,只是让我感到奇怪的是,您在两个答案上都问了同一个问题,对我来说,这似乎是您想指出您的聪明才智,所以我想报答;- )
    【解决方案3】:

    您可以在 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>
    

    【讨论】:

    • 如果 url 是 http://www. siteA.com/?或者,“https://www.siteA.com/http://.com/”?
    • 这段代码很有效
    • 不客气。
    【解决方案4】:

    在纯 HTML 中不可能有这个逻辑,尽管一个小的 javascript sn-p 就足够了

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

    【讨论】:

    • 如果网址以“www.”开头怎么办?
    • 那么它可以很容易地添加到列表中。该代码仅显示了一个解决方案,如果 OP 想要一个能够在客户端 JavaScript 中进行各种漂亮重定向的巧妙解决方案,它会在问题中这么说。我们都没有包含任何请求参数和/或锚点保存,因为这不是问题的一部分。
    猜你喜欢
    • 2012-07-25
    • 2021-11-18
    • 2013-07-16
    • 2021-01-26
    • 2010-12-27
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    相关资源
    最近更新 更多