Redirect according to the domain

0

Hello friends, I'm looking for the right way to redirect according to where the visit came from.

I have this code:

 <script>
var result = document.referrer.split( '/' ).indexOf("https://ged-botxtrem.rhcloud.com/");;
if (result >= 0){
    alert("Correcto");
} else {
    alert("Incorrecto");
    location.href = "https://www.google.com/";
}
</script>

He works fine but it turns out that when the url is:

https://ged-botxtrem.rhcloud.com/index.php

Or another variant. That is not just the domain.

Redirects the same as it is not the correct domain.

I hope some help. Thank you.

    
asked by BotXtrem Solutions 27.06.2017 в 01:40
source

2 answers

0

Why do not you use regex?

var url = document.referrer;
var rgx;

rgx = new RegExp("https?\:\/\/es\.stackoverflow\.com\/");

//En tu caso
//rgx = new RegExp("https?:\/\/ged\-botxtrem\.rhcloud\.com\/");

if(rgx.test(url))
  console.log("Correcto");
else
  console.log("Incorrecto");
    
answered by 28.06.2017 / 02:01
source
1

indexOf

The indexOf () method returns the index, within the String object that makes the call, of the first occurrence of the specified value, starting the search from index Search; or -1 if that value is not found.

That is, if https://ged-botxtrem.rhcloud.com/index.php contains https://ged-botxtrem.rhcloud.com/ and in your example it will always be true, it will return a value other than -1.

If you want to verify that the strings match or are the same , you should compare the strings with localeCompare

 <script>
var result = document.referrer.split( '/' ).localeCompare("https://ged-botxtrem.rhcloud.com/");;
if (result >= 0){
    alert("Correcto");
} else {
    alert("Incorrecto");
    location.href = "https://www.google.com/";
}
</script>
    
answered by 27.06.2017 в 02:10