问题原文
原帖链接 · 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
