通过 .htaccess 密码保护允许单个 URL

Avatar of Chris Coyier
Chris Coyier

此代码对于多环境设置(暂存、生产等)很有用,它允许您保持 htaccess 文件同步,同时在开发环境或除生产环境之外的任何环境中维护 htpasswd。

#allows a single uri through the .htaccess password protection
SetEnvIf Request_URI "/testing_uri$" test_uri

#allows everything if its on a certain host
SetEnvIf HOST "^testing.yoursite.com" testing_url
SetEnvIf HOST "^yoursite.com" live_url
Order Deny,Allow

AuthName "Restricted Area"
AuthType Basic
AuthUserFile /path/to/your/.htpasswd
AuthGroupFile /
Require valid-user

#Allow valid-user
Deny from all
Allow from env=test_uri
Allow from env=testing_url
Allow from env=live_url
Satisfy any