Sunday 6 May 2012

Apache restrict access to selected directories based on IP address

Apache let's you restrict access to selected directories using the mod_authz_host module.
To restrict access one option is to modify the httpd.conf file (in your Apache root folder). By default, your Apache configuration allows access to anyone. My original httpd.conf file has a section like the following:


<Directory "C:/xampp/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks Includes ExecCGI


    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All


    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all


</Directory>


Note that an .htaccess file placed in a selected directory will override the access set in the httpd.conf file.


C:/xampp/htdocs is my Apache root folder so if I want to restrict access to only localhost, the restriction would be:


<Directory "C:/xampp/htdocs">
    
    Order deny,allow
    Allow from 127.0.0.1


</Directory>


after making changes, restart Apache. You first deny access to all (which is the default behaviour) and then allow access to localhost.



No comments:

Post a Comment