大网数据广告活动
服务器运维问答

Apache .htpasswd not applied to subdirectories

· 52CE 我爱测

I have several WordPress staging sites placed in a staging directory. The staging directory holds a .htaccess and .htpasswd .htaccess: AuthType Basic AuthName "restricted area" AuthUserFile /var/www/staging/.htpasswd require vali…

问题原文

原帖链接 · Daniel Klose · CC BY-SA 3.0

I have several WordPress staging sites placed in a staging directory. The staging directory holds a .htaccess and .htpasswd

.htaccess:

AuthType Basic
AuthName "restricted area"
AuthUserFile /var/www/staging/.htpasswd
require valid-user

.htpasswd

username:PdadsaasdMehzdsadwad

When I place a file in this directory, the authentication process triggers appropriately, however when I place a WordPress site in the subdirectory, the authentication is not showing.

Example WP site: /var/www/staging/some-wp-site

.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>

WP Site Vhost:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName somewpsite.com
        ServerAlias somewpsite.com
        DocumentRoot /var/www/staging/some-wp-site
        <Directory />
                AllowOverride All
        </Directory>
        <Directory /var/www/staging/some-wp-site>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride all
                Require all granted
        </Directory>
</VirtualHost>

my apache.conf

<Directory /var/www/staging>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
</Directory>

I suspect I need to modify the vhost to use the authentication, however since I have quite a few sites in that directory I would prefer if there was a globl rul that simply triggers for any files placed in this subdirectory, no matter what another configuration says. Is this possible?

已采纳解答

原帖链接 · HBruijn · CC BY-SA 3.0

The problem is indeed that you are using .htaccess files even though you have access to the main Apache configuration files.

In general, you should only use .htaccess files when you don't have access to the main server configuration file. There is, for example, a common misconception that user authentication should always be done in .htaccess files, and, in more recent years, another misconception that mod_rewrite directives must go in .htaccess files. This is simply not the case. https://httpd.apache.org/docs/2.4/howto/htaccess.html

In this case the unexpected behavior that the .htaccess file in /var/www/staging is not applied happens because, as far as I know, Apache will only search for and apply any .htaccess files found in the DocumentRoot directory and below. I.e. only /var/www/staging/some-wp-site and subdirectories thereof.

I am fairly certain that if you create a directory block in your main httpd.conf to require Authentication on /var/www/staging it will also apply to all subdirectories and every virtual host with a DocumentRoot in there.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-04-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。