3

I've got a Landingpage: "https://www.example.com" and an Application: "https://app.example.com/something/" and on some user actions like logout i need to redirect the user back to the original landingpage. my hard-coded solution does work:

window.location = "https://www.example.com"

but this is bad practice and annoying in other environments like my local development(http://localhost:8082/something - for the app, no local landingpage). is there some good and easy solution to redirect to just the base-domain of my current site(example.com redirects to www.example.com by default)?

1
  • This is often taken care of by changing the settings in the web server. In many hosting platforms (I use GoDaddy), there are settings for this. Commented Nov 21, 2016 at 15:39

1 Answer 1

5

I would then use

var hostname = location.hostname;
window.location = `https://${hostname}`;

source: Determine domain name in JavaScript?

also: http://www.w3schools.com/jsref/prop_loc_hostname.asp

edit: for the port:

http://www.w3schools.com/jsref/prop_loc_port.asp

You could use

var hostname = location.hostname;
var port = location.port;
window.location = `https://${hostname}:${port}`
4
  • i tried that and there are 2 problems: 1. im redirectet to https(even if im local on http) and 2. the port(8082) is lost. i know i could write some regex and strig replace but this seems a bit too complex for a simple redirect....
    – ahe
    Commented Nov 21, 2016 at 15:53
  • what about instead of "https://${hostname}" you use "http://${hostname}"?
    – Besto
    Commented Nov 21, 2016 at 15:58
  • there is still the problem with my port :( the port doesnt seem to be part of location.hostname, but location.port holds it. maybe i'll run my local server at port 80..
    – ahe
    Commented Nov 21, 2016 at 16:12
  • also: w3schools.com/jsref/prop_loc_port.asp You could use var port = location.port; and then "window.location = "https://${hostname}:${port}";
    – Besto
    Commented Nov 21, 2016 at 16:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.