Redirect to new URL and keep path Jquery
I want to redirect to another URL but keep trailing path.
I use window.location.href;
to redirect to another URL, but how do I keep path. What I want is illustrated below.
ORIGINAL URL
<script>
window.location.href="www.example.com/the_path";
</script>
to be redirected too
<script>
window.location.href="mobile.example.com/the_path";
</script>
I want to the keep "the_path", I need that part.
Thanks in advance.
Solution 1:
You can use URL constructor to create a new url using the current location.href then simply change the host
// create a path in this demo only
history.pushState(null,null, '/the_path?foo=bar')
const url = new URL(location.href)
url.hostname = 'mobile.example.com';
console.log(url.href)