【问题标题】:Replace subdomain name with other subdomain Using JavaScript?用其他子域替换子域名使用 JavaScript?
【发布时间】:2013-04-11 16:55:15
【问题描述】:

我正在尝试将子域名从“news.domain.com/path/..”替换为“mobile.domain.com/path/. .",使用 JavaScript

知道如何实现吗?

【问题讨论】:

  • 正如其他人所暗示的那样,您能否更清楚地了解想要实现的目标:您是将用户的浏览器重定向到新的 URL,还是只想知道如何转换第一个字符串变成第二个?
  • 我想把url中的子域名news改为mobile,所以显示的是手机版的页面。

标签: javascript string replace


【解决方案1】:

如果你想通过 JS 将用户发送到新的 url - 使用document.location = "mobile.domain.com/path/.."

【讨论】:

    【解决方案2】:

    您不能替换子域。您可以使用 javascript 进行重定向。

    <script type="text/javascript">
    <!--
    window.location = "http://mobile.domain.com/path/to/file.html"
    //-->
    </script>
    

    【讨论】:

    • 我知道要重定向域,我想在 url 中将单词 news 更改为 mobile
    【解决方案3】:

    我假设您要将通用格式 xxxx.domain.com/... 的字符串更改为 mobile.domain.com/...。这个正则表达式应该在 JavaScript 中完成:

    var oldPath = "news.domain.com/path/";
    var newPath = oldPath.replace(/^[^.]*/, 'mobile')
    

    【讨论】:

    • 如何获取当前路径(旧路径),因为 url 是动态的
    • 我试过这个 var oldPath = window.location.href; var newPath = oldPath.replace(/^[^.]*/, 'mobile');文档位置=新路径;但没有运气。
    • @Bala, window.location.href 会给你一个完全合格的 URL(例如http://news.domain.com/path)。运行上述代码时,您需要考虑 http:// 前缀。
    【解决方案4】:

    我尝试使用 java 脚本但没有运气,就我而言,我在 .httaccess 文件中使用以下代码

    RewriteCond %{HTTP_USER_AGENT} "iphone|ipod|android" [NC]
    RewriteCond %{HTTP_HOST} !^mobile.domain.com
    RewriteRule ^(.*)$ http://mobile.domain.com/ [L,R=302]
    

    它将“新闻”子域替换为“移动”子域。希望对大家有所帮助。

    【讨论】:

    • 当问题明确要求 JavaScript 解决方案时,这怎么可能是正确的。
    • @moefinley 至少当时我已经实现了这个方法,所以对我帮助很大
    【解决方案5】:

    参考 FixMaker 对其answer 的评论:

    window.location.href 将为您提供一个完全限定的 URL(例如 http://news.domain.com/path)。运行上述代码时需要考虑 http:// 前缀

    处理请求方案(http/https)的合适正则表达式如下:

    function replaceSubdomain(url, subdomain){
        return url.replace(/^(https?:\/\/)(www\.)?([^.])*/, `$1$2${subdomain}`);
    }
    
    let url1 = 'https://sub-bar.main.com';
    let url2 = 'https://www.sub-bar.main.com';
    
    console.log(replaceSubdomain(url1, 'foobar'));
    console.log(replaceSubdomain(url2, 'foobar'));

    【讨论】:

      【解决方案6】:

      这应该在正常情况下工作:

      "http://news.domain.com/path/..".replace(/(:\/\/\w+\.)/, "://mobile.")
      

      使用以下内容添加额外的验证级别:

      function replaceSubdomain(url, toSubdomain) {
          const replace = "://" + toSubdomain + ".";
      
          // Prepend http://
          if (!/^\w*:\/\//.test(url)) {
              url = "http://" + url;
          }
      
          // Check if we got a subdomain in url
          if (url.match(/\.\w*\b/g).length > 1) {
              return url.replace(/(:\/\/\w+\.)/, replace)
          }
      
          return url.replace(/:\/\/(\w*\.)/, `${replace}$1`)
      }
      
      console.log(replaceSubdomain("example.com", "mobile"));
      console.log(replaceSubdomain("http://example.com:4000", "mobile"));
      console.log(replaceSubdomain("www.example.com:4000", "mobile"));
      console.log(replaceSubdomain("https://www.example.com", "mobile"));
      console.log(replaceSubdomain("sub.example.com", "mobile"));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-13
        • 2012-11-02
        • 2014-05-20
        • 1970-01-01
        • 1970-01-01
        • 2020-06-04
        • 2011-02-10
        相关资源
        最近更新 更多