首页 文章

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

提问于
浏览
1

我想禁用目录的访问,但不禁用.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]

1 回答

  • 5

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

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

    Allow from none
    Deny from all
    

    并在您的 public_html 文件夹中添加一个内容:

    Allow from all
    

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

    编辑:

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

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

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

相关问题