execute index.php but not other .php

Discussion in 'Apache' started by jwzumwalt, Sep 23, 2012.

  1. #1
    I have a section on my server with public php snippets.
    I would like to have index.php execute but not other
    php files. I have attempted to set Unix file permissions
    but since php files are read only 644 anyway this is
    not the solution. I have tried various combinations of
    the .htaccess settings shown here and get for different
    results.

    1) all php file download
    2) all php files are forbidden
    3) all php files execute
    4) index executes but other php are forbidden


    What I am hoping for is to have all but index.php
    shown as a text file. Thanks for the help

    <FilesMatch "\.(htm|html|php)$">
    SetHandler application/x-httpd-php
    </FilesMatch>


    <FilesMatch "\.php$">
    Order Allow,Deny
    Deny from all
    </FilesMatch>


    <FilesMatch "index\.php$">
    Order Allow,Deny
    Allow from all
    </FilesMatch>


    <FilesMatch "index.php$">
    Order Allow,Deny
    Allow from all
    </FilesMatch>
     
    jwzumwalt, Sep 23, 2012 IP
  2. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #2
    I'm not sure what you exactly wanted AFTER not allowing php files to run, to run a download on those scripts and have the visitor download each php/html except index.php file, OR actually just forbid the execution. So here are both options.

    To have index.php execute on the server, and all other php and html files download to client browser:
    
    <FilesMatch "\b(?!.*index\.php$)(.+)(?i)\.(htm|html|php|php4|php5)$">
    SetHandler application/octet-stream
    </FilesMatch>
    
    Code (markup):
    To have index.php execute, and all other php files return "403 Forbidden":
    
    <FilesMatch "(?i)\.(php|php4|php5)$"> 
        Order Allow,Deny 
        Deny from all 
    </FilesMatch> 
    <FilesMatch "index\.php$"> 
        Order Allow,Deny 
        Allow from all 
    </FilesMatch>
    
    Code (markup):
    Some further testing should be applied to both snippets... but I believe it should be working fine.

    Cheers!
     
    Last edited: Oct 1, 2012
    pr0t0n, Oct 1, 2012 IP