如何做有条件的.htaccess密码保护

Clo*_*ner 5 .htaccess conditional passwd setenv ifdefine

我正在尝试使用.htaccess密码保护特定网址.不同的URL指向相同的文件但具有不同的工作方式.我现在需要密码保护只有一个网址.我正在尝试使用setenvif这样做但它似乎不起作用.我可能不完全理解apache setenv模块的用途或用途.

这是我的代码似乎不起作用

SetEnvIfNoCase Host "topasswordprotect\.domain\.com$" protecturl
<IfDefine protecturl>
  Require valid-user
  AuthName "Please enter your name and password"
  AuthType Basic
  AuthUserFile .htpasswd
</IfDefine>
Run Code Online (Sandbox Code Playgroud)

Clo*_*ner 4

我终于弄明白了。我确实不得不使用 mod_rewrite 引擎。我决心找到一个不需要创建额外文件或目录的解决方案。

相同的文件,没有额外的目录,一个 .htaccess:

# Case we are in the protected area!
RewriteCond %{REQUEST_FILENAME} index.php [OR]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^(\/){1}$
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^(\/){1}$
RewriteCond %{HTTP_HOST} ^(protected)\.mydomain\.com$
RewriteRule ^(.*)$ admin.php%{REQUEST_URI} [L]

#default rewrite
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

#only protect access to admin.php which is a symlink to index.php
<FilesMatch "admin.php">
  AuthUserFile .htpasswd
  AuthName EnterPassword
  AuthType Basic
  require valid-user
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)