Access by url referral only

Discussion in 'Programming' started by adam1987, Feb 18, 2007.

  1. #1
    I want to only allow access to a page on my website by url referral only.

    eg, the file to be displayed is: www.mydomain.com/showpage.html

    I dont want people to be able to type the exact url, i only want the page to be viewed by referral from eg: www.mydomain.com/main.html

    Hope that makes sense, if i could re-direct people who try and access the url without referral that would be great !!

    Hope someone can help !
     
    adam1987, Feb 18, 2007 IP
  2. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Try the following htaccess code to redirect all visitors without a referer to another page
    
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} ^$
    RewriteRule (.*) http://www.yourdomain.com/path/to/redirect/
    
    Code (markup):
    On the other hand, if you want only visitors referred by some particular domain/url to access a page try
    
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?allowed-domain1.com [NC]
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?allowed-domain2.com/allowed/url/ [NC]
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?allowed-domain3.com/allowed/page.html[NC]
    RewriteRule (.*) http://www.yourdomain.com/path/to/redirect/
    
    Code (markup):
    This allows visitors referred by the above three domains/urls to access the contents. Others are redirected
     
    Aragorn, Feb 21, 2007 IP
  3. adam1987

    adam1987 Well-Known Member

    Messages:
    714
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Hi , I have tried the second code and it seems to be re-directing everything...
     
    adam1987, Feb 21, 2007 IP
  4. adam1987

    adam1987 Well-Known Member

    Messages:
    714
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #4
    adam1987, Feb 21, 2007 IP
  5. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Try this code. Put it in http://www.bluepage.com/videos/
    
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?redpage.com [NC]
    RewriteRule (.*) http://www.greenpage.com [NC,R,L]
    
    Code (markup):
    Change the last line to
    
    RewriteRule (.*)/index.html http://www.greenpage.com [NC,R,L]
    
    Code (markup):
    to redirect the visitors to http://www.bluepage.com/videos/index.html only. Or else every pages in the directory gets affected.
     
    Aragorn, Feb 21, 2007 IP
  6. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Use PHP, HTTP_REFERER - http://php.net/reserved.variables

    <?php
      if ($_SERVER['HTTP_REFERER'] == 'http://www.redpage.com') {
        # welcome buddy!
      } else {
        # hey, where do you think you are going?!?
      }
    ?>
    PHP:
    Please note that, as stated here http://php.net/reserved.variables:
    "The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted."
     
    picouli, Feb 21, 2007 IP
  7. adam1987

    adam1987 Well-Known Member

    Messages:
    714
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Thanks for all the info i will give it a try, another question.

    Could i have the content displayed on my website dependant upon the reffer url ?
     
    adam1987, Feb 21, 2007 IP
  8. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Of course - I don't know if you are using PHP, but using the same trick above you could do something like:
    <?php
      if ($_SERVER['HTTP_REFERER'] == 'http://www.redpage.com') {
        include 'redpage.html';
      } elseif ($_SERVER['HTTP_REFERER'] == 'http://www.yellowpage.com') {
        include 'yellowpage.html';
      } else {
        # all other referrers
      }
    ?>
    PHP:
    HTH, cheers! :)
     
    picouli, Feb 21, 2007 IP
  9. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Possible with htaccess too :)

    Suppose the file for referrals from redpages.com are in yourdomain.com/redpages/ and those for bluepages.com are in the directory bluepage then use
    
    RewriteCond %{HTTP_REFERER} ^http://(www\.)?redpage.com [NC]
    RewriteRule (.*) redpages/$1 [NC,R,L]
    RewriteCond %{HTTP_REFERER} ^http://(www\.)?bluepage.com [NC]
    RewriteRule (.*) bluepages/$1 [NC,R,L]
    
    Code (markup):
    Here any request for a file will be mapped to the directory corresponding to that referer.
    If you are using a server side script like php, then I would recommend picouli's method over this.
     
    Aragorn, Feb 21, 2007 IP
  10. adam1987

    adam1987 Well-Known Member

    Messages:
    714
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #10
    Thanks for the advice... I’m getting closer to what I want to do here!!

    The file I will be using will be called index.php

    The code above by picouli works OK...BUT…What I would like to do is have all my content in the index file and include a different stylesheets depending on the refferer.
     
    adam1987, Feb 21, 2007 IP
  11. bscdesign.com

    bscdesign.com Active Member

    Messages:
    681
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #11
    If you make your layout dependent on CSS then in index.php where the HTML head tags are, inbetween them put:
    <?php
      if ($_SERVER['HTTP_REFERER'] == 'http://www.redpage.com') {
        <link href="redstyle.css" rel="stylesheet" type="text/css" />
      } else {
        <link href="yellowstyle.css" rel="stylesheet" type="text/css" />
      }
    ?>
    PHP:
    And have the 2 CSS files display a different layout or style for your site.
     
    bscdesign.com, Feb 21, 2007 IP