我想禁用目录的访问,但不禁用.htaccess的子目录

Ken*_*iau 1 apache url .htaccess

我想禁用目录的访问,但不禁用.htaccess的子目录

我已经编写了一个重写规则来将所有内容重定向到子目录(public_html),但如果用户知道url,它的父级仍然可访问,我想禁用当前目录的访问而不是子目录

    # 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]

Tam*_*rta 5

您必须在主服务器配置中使用两个.htaccess文件或两个目录部分(如果您的服务器允许.htaccess文件,则第一个解决方案更容易).

在你的root中输入一个.htaccess,内容如下:

Allow from none
Deny from all
Run Code Online (Sandbox Code Playgroud)

并在您的public_html文件夹中放入一个内容:

Allow from all
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您的根目录中无法访问任何内容,但您可以public_html覆盖此内容,并且可以访问所有内容.

编辑:

你可以通过将这个.htacces放在你的root中来实现你想要的:

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_URI} !^/public_html/
RewriteRule ^(.*)$ public_html/$1
Run Code Online (Sandbox Code Playgroud)

并将原始版本放入public_html并对其进行修改以反映它现在位于public_html文件夹中.我发布的这个配置负责跳过根目录,并伪装public_html成根目录.