6

I'm trying to replace the subdomain name from "news.domain.com/path/.." to "mobile.domain.com/path/..", using JavaScript

Any idea how to achieve this?

FixMaker
  • 3,759
  • 3
  • 26
  • 44
Bala
  • 362
  • 1
  • 9
  • 25
  • 1
    As others have implied, can you be a bit more clear about what want to achieve: do you want to redirect the user's browser to the new URL, or do you just want to know how to transform the first string into the second? – FixMaker Apr 18 '13 at 14:14
  • i want to replace the subdomain name news instead mobile in the url, so it shows the mobile version of the page. – Bala Apr 18 '13 at 14:21

6 Answers6

6

I'm assuming that you want to change a string in the generic format xxxx.domain.com/... into mobile.domain.com/.... This regexp should do it in JavaScript:

var oldPath = "news.domain.com/path/";
var newPath = oldPath.replace(/^[^.]*/, 'mobile')
FixMaker
  • 3,759
  • 3
  • 26
  • 44
  • how do i get the current path(old path), because the url is dynamic – Bala Apr 18 '13 at 14:27
  • i tried this var oldPath = window.location.href; var newPath = oldPath.replace(/^[^.]*/, 'mobile'); document.location = newPath; but no luck. – Bala Apr 18 '13 at 14:40
  • 1
    @Bala, `window.location.href` will give you a fully qualified URL (e.g. `http://news.domain.com/path`). You'll need to take into account the `http://` prefix when running the above code. – FixMaker Apr 18 '13 at 16:01
5

This should work in normal cases:

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

Use following to add an extra level of validation:

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"));
AamirR
  • 11,672
  • 4
  • 59
  • 73
-1

If you want to send user to new url via JS - use document.location = "mobile.domain.com/path/..".

Dmitry Volokh
  • 1,630
  • 16
  • 28
-1

In reference to FixMaker's comment on his answer:

window.location.href will give you a fully qualified URL (e.g. http://news.domain.com/path). You'll need to take into account the http:// prefix when running the above code

A suitable regular expression to handle the request scheme (http/https) is as follows:

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'));
chukkwagon
  • 614
  • 6
  • 11
-3

You cannot replace a subdomain. You can redirect using javascript.

<script type="text/javascript">
<!--
window.location = "http://mobile.domain.com/path/to/file.html"
//-->
</script>
SPRBRN
  • 2,406
  • 4
  • 35
  • 48
-3

I tried using java script but no luck and for my case i use the below code in .httaccess file

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

it will replace "news" sub domain to "mobile" sub domain. hope it will help any one.

Bala
  • 362
  • 1
  • 9
  • 25