deny access to .php files to visitors

Discussion in 'PHP' started by el07694, Nov 17, 2011.

  1. #1
    Hi guys..
    First post here!!
    I want use mod_rewrite module of apache to block access to .php files.
    The only .php file witch the visitors can execute is hosted to http://www.blabla.com/php/index.php
    Well first of all i want to make this my index file so:
    
    DirectoryIndex php/index.php
    
    Code (markup):
    The next i want to do is to make a 404 error page
    
    DirectoryIndex php/index.php
    ErrorDocument 404 php/error.php
    
    Code (markup):
    Well now, i want visitors can access php/index.php only from http://www.blabla.com/ but not from http://www.blabla.com/php/index.php, so
    
    Options +FollowSymLinks
    RewriteEngine On
    
    DirectoryIndex php/index.php
    ErrorDocument 404 php/error.php
    
    #see if the request is to the file is from visitor
    RewriteCond %{REMOTE_ADDR} !127\.0\.0\.1
    #if it's from visitor do the following rule
    RewriteRule php/index\.php e404
    
    Code (markup):
    I have install xampp to my computer and when i type http://localhost/ i see php/index.php and when i type http://192.168.1.5 i see the error page.
    (192.168.1.5 is my LAN ip-address :p)
    I want http://192.168.1.5/php/index.php to redirect to http://192.168.1.5/php/error.php and http://192.168.1.5/ to http://192.168.1.5/php/index.php
    How i can do this?
     
    el07694, Nov 17, 2011 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    Options +FollowSymLinks
    RewriteEngine On
    
    DirectoryIndex http://192.168.1.5/php/index.php
    ErrorDocument 404 php/error.php
    
    #see if the request is to the file is from visitor
    RewriteCond %{REMOTE_ADDR} !127\.0\.0\.1
    #if it's from visitor do the following rule
    RewriteRule php/index\.php e404
    Code (markup):
    and in index.php

    <?php
    header("Location: http://192.168.1.5/php/error.php");
    ?>
    
    PHP:
    Just a thought, there is probably an easier way of doing it, method is not tested
     
    Anveto, Nov 17, 2011 IP
  3. el07694

    el07694 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Maybe you didn't understand what i want to do..
    192.168.1.5 want to be any ip-address (even server-ip)
    When a visitor clicks www .blabla. com/ he goes to www. blabla. com/php/index.php
    and when he clicks www .blabla. com/php/index.php he goes to 404 error page (SEO friendly if possible).
    Thx for the reply man!! :)

    I think one more ReWriteCond for REQUEST_URI with the neccessery flags should give me the answer
     
    Last edited: Nov 17, 2011
    el07694, Nov 17, 2011 IP
  4. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #4
    Sorry, might have skimmed through it a bit quick.

    I don't know about the .htaccess file but in index.php you could do

    
    <?php
    if ($_SERVER['HTTP_REFERER'] != 'http://www.blabla.com/') {
    header("Location: error.php");
    }
    ?>
    
    PHP:
    Can't say it would work 100% of the time or if it works :) But maybe something along those lines would

    or you could do a window.location with javascript but its not as reliable
     
    Anveto, Nov 17, 2011 IP
  5. el07694

    el07694 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Options +FollowSymlinks
    RewriteEngine on

    RewriteRule ^/?$ /php/index.php [S=1]
    RewriteCond %{REQUEST_URI} =/php/index\.php
    RewriteRule (.*)? /php/error.php [R=404]

    bla-bla.com to bla-bla.com/php/index.php
    bla-bla.com/php/index.php to bla-bla.com/php/error.php
    DOES NOT WORK :eek:
     
    el07694, Nov 17, 2011 IP
  6. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #6
    Yea, i was just saying you could do it with php instead :) I'm not that good with htaccess
     
    Anveto, Nov 18, 2011 IP
  7. el07694

    el07694 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Notice: Undefined index: HTTP_REFERER
    ?????
     
    el07694, Nov 18, 2011 IP
  8. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #8
    If you are going to the site directly (not from another page) there won't be a referrer, therefore undefined
     
    Anveto, Nov 18, 2011 IP