0

I have a website (let's call it www.abc.com) that I need to put a redirect on. The landing page is www.abc.com/index.html so the redirect needs to be on that page. The redirect needs to work if the referrer is null (someone types the domain in directly) or the referrer is coming from outside of the abc.com domain (e.g. google.com) in which it should redirect to www.abc.com/splash/index.html.

The closest thing I have come up with is the below but it is not working. When I go to abc.com it goes to the splash page which is good. But then when I click on the link on the splash page to go to abc.com it redirects back to the splash page again. Infinite loop. Not sure what to do here and any help is appreciated!

<script type="text/javascript" charset="utf-8">
   if (document.referrer == '' || document.referrer.indexOf('www.abc.com') != -1) {
   top.location="http://www.abc.com/splash/index.html";
   }
</script>
slybitz
  • 689
  • 1
  • 7
  • 18
  • 1
    HTTP_REFERER is not guaranteed to be present or accurate. Don't rely on it. –  Nov 13 '13 at 23:14
  • 1
    Browsers do not always provide a document.referrer. If the referrer is from a https it is blocked, or if the security settings in the broswer block it none is provided. These people will never get past the splash page and will be cut off from being able to use your site. – Wayne Nov 13 '13 at 23:17
  • Thanks guys, this is helpful. I didn't realize that coming from a secure site to a non secure site would remove the referrer. That makes sense that when I come from Google to my site it does not give me a referrer. I'll have to find another approach... – slybitz Nov 14 '13 at 17:12

2 Answers2

1
document.referrer.indexOf('www.abc.com') != -1

states that the referrer does include that string. If I understand your text correctly you want the opposite of that.

document.referrer.indexOf('www.abc.com') < 0

EDIT Or, to do justice to URL syntax:

document.referrer.match(/^http:\/\/www\.abc\.com.*/)
Christian Fritz
  • 20,641
  • 3
  • 42
  • 71
  • Well sort of. The issue I'm having is that when I use "< 0" and I search for my site via google and click the link from within google search results it goes to the www.abc.com/index.html page instead of the splash page. The way I have it above (i.e., !=-1) it does not do that - it goes to the splash page. But then the infinite loop issue I talked about above appears when I do it this way. So how do I get it to go to the splash page both ways: (1) when I enter in the address directly to address bar and 2) click the link via google search or similar? – slybitz Nov 13 '13 at 23:28
  • Only because it is working when do it wrong, doesn't mean it's correct! Your use of .indexOf to determine whether the referrer is outside or not is naive. You really ought to check that the actual domain name is not abc.com. Perhaps see this: http://stackoverflow.com/questions/4140324/parse-url-with-javascript – Christian Fritz Nov 14 '13 at 00:31
  • i do appreciate the help but it's actually not naive. checking for the actual domain using location.hostname.match will tell me the domain i'm currently on which will always be www.abc.com. what good is that? i need the referrer, not the domain where anything outside of abc.com as the referrer should hit the splash. – slybitz Nov 14 '13 at 16:40
  • it is naive, because both otherdomain.com/q?goto=www.abc.com and http://www.abc.com will match your .indexOf search, i.e., you can't use it to distinguish between these two scenarios. You need to parse the URL. I'm not suggesting to use location.hostname, but there are other pointers int hat thread on how to parse a url according to its syntax. – Christian Fritz Nov 14 '13 at 17:50
  • OK, i've made my answer more explicit. – Christian Fritz Nov 14 '13 at 17:54
1

I think you mean

if (document.referrer == '' || document.referrer.indexOf('www.abc.com') == -1)

== instead of !=. indexOf returns -1 if it is not found.

Robbie Wxyz
  • 7,671
  • 2
  • 32
  • 47