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?

2
  • 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? Commented Apr 18, 2013 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. Commented Apr 18, 2013 at 14:21

6 Answers 6

7

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')
Sign up to request clarification or add additional context in comments.

3 Comments

how do i get the current path(old path), because the url is dynamic
i tried this var oldPath = window.location.href; var newPath = oldPath.replace(/^[^.]*/, 'mobile'); document.location = newPath; but no luck.
@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.
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"));

2 Comments

Its not considering special characters in subdomain like underscore or dash
Regex to handle subdomains that include special characters: /:\/\/([\w_-]*\.)/
-1

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

Comments

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

Comments

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

1 Comment

I know to redirect the domain, i want to change the word news to mobile in url
-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.

2 Comments

How can this be correct when the question clearly asks for a JavaScript solution.
@moefinley atleast I achieved this method by that time, so it helped me a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.