Redirect based on referrer URL

2019-01-18 23:23发布

In my site I have a password protected page containing some links to other sites also operated by myself that cannot be password protected. I would like to place a HTML code onto one of the other sites I operate that checks that the person arriving at the page has been referred from the URL of the 'Links Page'.

(I understand that this is not a secure option)

Summary:

If Referrer = 'Links Page URL' *then* Do nothing *Else* Redirect: www.google.com.

Does anyone know a simple HTML/ Javascript code that I can copy and paste into my site?

3条回答
Rolldiameter
2楼-- · 2019-01-19 00:07

Try this

    function url(url){
      return url.match(/:\/\/(.[^/]+)/)[1];
    }

    function check()
    {
      var ref = document.referrer;
      if(url(ref) =='www.google.com')
      {
          // do something
      }
      else
      {
         // redirect
         window.location.href = 'http://yourDomain.com';
      }
   }
查看更多
再贱就再见
3楼-- · 2019-01-19 00:09

I found document.referrer doesn't work for me, but location.href works:

if (location.href != "http://yoursite/index.html") {
    location.replace("http://yoursite/index.html");
}
查看更多
手持菜刀,她持情操
4楼-- · 2019-01-19 00:10
if (document.referrer !== "http://www.stackoverflow.com") {
    window.location.href = "http://www.google.com";
}

Or you can use regular expressions to check the referrer.

Anyway, this solution is really, really unsafe. You can just turn off JavaScript in your browser and won't be redirected...

查看更多
登录 后发表回答