简体   繁体   中英

Replace URL in address bar with a subdomain withOUT redirecting

I have total control over my entire domain and its subdomains, and I want to replace the URL displayed in the address bar with a subdomain but not redirect the page.

For example, when I load subdomain.mydomain.com?register I want the address bar to display register.mydomain.com without redirecting.

ALL pages and subdomains of mydomain.com already have document.domain = 'mydomain.com'; as the first thing in the $(window).load(function () { code of its external .js file.

I've tried history.pushState("", "", '/'); to strip away anything after the subdomain.mydomain.com (which works), and I've been able to display something after the / (which also works), but I can't get the displayed subdomain to change before the / .

How do I do this? Is it possible at all? If so, does it rely on document.domain ? And if it does, am I executing document.domain properly??


Should I put

 <script type="text/javascript">
 document.domain = 'mydomain.com';
 </script>

by itself in the <head> of my HTML files, or will it work just fine being in the $(window).load(function () { code of its external .js file?

I'm not sure about domain/subdomain things, but I have a page where a user can click multiple items and depending on his/her input, a URL that is displayed in browser's address bar changes. There is how I do it without redirecting:

var newurl = window.location.protocol 
    + "//" + window.location.host + window.location.pathname 
    + StrippedQueryString + '&H=' + Parameter;

if (history.replaceState) {
    window.history.replaceState({ path: newurl }, '', newurl);
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM