如何使Basic Auth排除重写的URL

时间:2012-12-19 10:25:26

标签: apache .htaccess authentication mod-rewrite .htpasswd

我在开发服务器上设置了基本身份验证。它是在我的httpd.conf文件中设置的,用于网站的VirtualHost。我必须设置它来排除某些目录,这些目录没有引起任何问题,一切正常。

问题在于排除已通过mod_rewrite文件中的.htaccess规则的网址。我的设置是我的所有网址都通过我的index.php文件,并从那里找到并运行相关代码。我尝试添加我想要排除的URL(/businesses/upload_logo),就像我做其他人一样,但它仍然需要身份验证。这就是我目前所拥有的:

...
<Location />
    SetEnvIf Request_URI "/businesses/upload_logo" noauth=1
    SetEnvIf Request_URI "/api/.*" noauth=1

    AuthType Basic
    AuthName "Private"
    AuthUserFile ****
    Require valid-user

    Order deny,allow
    Satisfy any
    Deny from all
    Allow from env=noauth
</Location>
....

我发现的问题类似于我here&amp; here但答案只能给我我正在尝试的内容。

我也想过其他可能的解决方案,但这些都是最后的选择。我想看看我现在的做法是否可行:

  • 在我的php代码中设置基本身份验证
    • 目前太麻烦
  • 将身份验证放在我的.htaccess文件中
    • 我不想这样做,因为我只希望在3台服务器之一上进行身份验证。我知道我可以使用更多SetEnvIf HOST ...但是我想看看它是否可以这样修复。

mod_rewrite规则:

...

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L,QSA]

3 个答案:

答案 0 :(得分:19)

尝试添加

Allow from env=REDIRECT_noauth

答案 1 :(得分:2)

对我来说,像这样的东西就像一个魅力:

<location />
        SetEnvIf Request_URI "/businesses/upload_logo" REDIRECT_noauth=1
        AuthType Basic
        AuthName "Restricted Files"
        AuthUserFile /etc/httpd/passwords/passwords
        Order Deny,Allow
        Satisfy any
        Deny from all
        Allow from env=REDIRECT_noauth
        Require user yournickname
</location>

答案 2 :(得分:-1)

根据你给出的内容,它应该有效,除非配置中的其他地方存在冲突的指令。

我做了类似的工作设置,只是我使用了文件系统路径而不是URI

我在这里添加它,希望你会发现它很有用

<VirtualHost *:8989 >
<IfModule mod_auth_basic.c>
 <Directory /var/www/html/vella-8989>
  # the auth block
  AuthType Basic
  AuthName "Please login."
  AuthUserFile /var/www/html/vella-8989/.htpasswd
  require valid-user

  Order Deny,Allow
  Satisfy any
  Deny from all
  Require valid-user
  Allow from env=noauth
</Directory>
</IfModule>
  # set an environtment variable "noauth" if the request has "/callbacks/"
  SetEnvIf Request_URI "/callbacks/" noauth=1
  ServerName vella.com
  ServerSignature off
</VirtualHost>