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

ahe
  • 128
  • 1
  • 9
  • 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. – Scott Marcus Nov 21 '16 at 15:39

1 Answers1

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}`
Besto
  • 388
  • 1
  • 13
  • 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 Nov 21 '16 at 15:53
  • what about instead of "https://${hostname}" you use "http://${hostname}"? – Besto Nov 21 '16 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 Nov 21 '16 at 16:12
  • also: http://www.w3schools.com/jsref/prop_loc_port.asp You could use var port = location.port; and then "window.location = "https://${hostname}:${port}"; – Besto Nov 21 '16 at 16:49