如何使用 htaccess 保护 Wordpress 安装中的目录?

How to protect a directory withing a Wordpress installation with htaccess?

我在 /root/website/ 有一个 wordpress 安装位置,在 /root/website/subpage/ 有一个单独的非 wordpress 子页面,所以我可以简单地通过 www.domain 访问它。com/subpage

我想用 htaccess 文件保护子页面,但在输入登录详细信息后我总是收到内部服务器错误 (500)。

难道不能有两个 htaccess 文件,还是我必须编辑 Wordpress htaccess 文件来保护子页面目录?

我的 Worpress 安装在其根目录中有一个 htaccess,其中包含:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

然后我的子页面目录中有以下 htaccess:

AuthType Basic
AuthName "NameHere"
AuthUserFile /root/website/subpage/.htpasswd
Require valid-user

当然,我已经使用在线生成器设置了一个 htpasswd 文件。

添加到 .htaccess 文件 public_html 或 publicDirectory:

您可以设置要允许的目录,示例如下:

<Directory /root/website>
# All access controls and authentication are disabled
# in this directory
Satisfy Any
Allow from all
</Directory>

<Directory /root/website/subpage>
AuthType Basic
AuthName "NameHere"
AuthUserFile /root/website/subpage/.htpasswd
Require valid-user
</Directory>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress