无法在 CodeIgniter 中使用密码保护特定 url

Can't password protect specific url in CodeIgniter

我需要在我的 codeigniter 站点上用密码保护特定的 route/url。

基础 url 看起来像这样:

https://staging.mysite.com/site-name

我想使用 .htaccess

用密码保护这个 url

https://staging.mysite.com/site-name/export

路线看起来像这样

$route['export']['get'] = 'ExportController/index';
$route['export']['post'] = 'ExportController/export';

对于类似的问题,我尝试了很多不同的答案,但我就是无法正常工作, 要么到处询问密码,要么根本不询问。

这是我的 .htaccess

RewriteEngine on


<IfModule mod_rewrite.c>

   RewriteEngine on
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php/ [L,QSA]
</IfModule>

SetEnvIfNoCase Request_URI ^/export SECURED

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

Satisfy    any
Order      Allow,Deny
Allow from all
Deny from env=SECURED

我认为问题可能出在这部分:

SetEnvIfNoCase Request_URI ^/export SECURED

因为我无法瞄准我想要的 url,这里是我尝试过的其他一些东西

SetEnvIfNoCase Request_URI ^/site-name/export SECURED
SetEnvIfNoCase Request_URI "^/site-name/export" SECURED
SetEnvIfNoCase Request_URI "^/export" SECURED
SetEnvIfNoCase Request_URI ^(.*)/export SECURED
SetEnvIfNoCase Request_URI .*/export$ SECURED
SetEnvIfNoCase Request_URI .*/export SECURED

编辑:

我也试过这样保护整个ExportController,密码提示没有出现在任何地方。

<Files ExportController>
AuthName "ExportController"
AuthType Basic
AuthUserFile "/home/something/path/to/.htpasswd"
require valid-user
</Files>

我最终这样做了:

<If "%{THE_REQUEST} =~ m#^GET /site-name/export#">
    AuthType Basic
    AuthName "Password Required"
    AuthUserFile /home/path/to/.htpasswd
    Require valid-user
</If>

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php/ [L,QSA]
</IfModule>