Redirect based on referrer URL

后端 未结 3 1179
庸人自扰
庸人自扰 2020-12-10 14:04

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

相关标签:
3条回答
  • 2020-12-10 14:49

    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");
    }
    
    0 讨论(0)
  • 2020-12-10 14:54

    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';
          }
       }
    
    0 讨论(0)
  • 2020-12-10 15:02
    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...

    0 讨论(0)
提交回复
热议问题