Forum Moderators: open
We'll set up something on the server at some point, but it often takes months for any change to go through...
As a temporary measure, we would like to force the use of a single domain for our users, but we want to keep the users on the page they were looking for.
We would like a JavaScript that will do the following:
* check if user is using domain.com or domaindev.com
* if not, reload the page they were viewing with domain.com
So, if they try to view wrongdomain.com/page.html, they will be redirected to domain.com/page.html.
<script type='text/javascript'>
var s = new String(window.location);
if ((s.indexOf("domain.com") == -1) && (s.indexOf("domaindev.com") == -1))
{
s = s.replace( /^(.*).com/,"http://domain.com");
window.location = s;
}</script>
The first line creates a string object "s" out of the window.location variable; which holds the current address of the page. The next line tests "s" for the presence of either domain.com or domaindev.com.
If neither are present, the condition is true and the replace is called; which uses a regular expression to chop everything upto and including the .com and replaces it with "http://domain.com" whilst leaving the rest of the URL (such as /page.html) intact.
Finally, setting window.location to the new value of s will make the browser redirect to the new location.
You will need to tweak this if you have some non .com's as your wrongdomains'.
It didn't work right away, because not all our domains end in ".com".
However, it did send me on the right path.
Here's the one that works:
<script type='text/javascript'>
var d = new String(window.location.host);
var p = new String(window.location.pathname);
var u = "http://" + d + p;
if ((u.indexOf("domain.gc.ca") == -1) && (u.indexOf("domaindev.com") == -1))
{
u = u.replace(location.host,"www.domain.gc.ca");
window.location = u;
}
</script>