2
votes

I want to disable the access of a directory but not the child directory with .htaccess

i already written a rewrite rules to redirect everything to a child directory (public_html), but its parent is still accessible if the user know the url, i want to disable the access of the current directory not the child directory

    # Turn on rewrites.
    RewriteEngine on

    # Only apply to URLs on this domain
    RewriteCond %{HTTP_HOST} ^(www.)?domain.com$

    # Only apply to URLs that aren't already under folder.
    RewriteCond %{REQUEST_URI} !^/folder/

    # Don't apply to URLs that go to existing files or folders.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Rewrite all those to insert /folder.
    RewriteRule ^(.*)$ /folder/$1

    # Also redirect the root folder.
    RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
    RewriteRule ^(/)?$ folder/index.php [L]
1

1 Answers

6
votes

You have to use two .htaccess file, or two Directory section in your main server config (the first solution is easier if your server permits .htaccess files).

Put in your root a .htaccess with content like this:

Allow from none
Deny from all

And put one in your public_html folder with content:

Allow from all

This way nothing will be accessible in your root, but your public_html overrides this, and in that, everything will be accessible.

Edit:

What you want can be accomplished by putting this .htacces in your root:

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_URI} !^/public_html/
RewriteRule ^(.*)$ public_html/$1

and putting your original one in public_html and make modifications to that to reflect that it is now in the public_html folder. This configuration that I posted is responsible to skip the root directory, and pretend like the public_html would be the root.