dqkf49487 2014-08-04 15:51
浏览 62
已采纳

.htaccess身份验证w /友好网址

I'm trying to setup my .htaccess file to enable basic authentication on our development website.

I want the authentication to work on all my site but for certain URL. We are using friendly URL on our site..

I would also like to bypass authentication for certain file types like png, css, etc...

What I have so far looks like this :

SetEnvIfNoCase Request_URI ^/upload/ NO_AUTH
AuthName "Restricted Area"
AuthType Basic
AuthUserFile /path/to/.htpasswd
Require valid-user
Satisfy  any
Order deny,allow
Deny from all
Allow from env=NO_AUTH
<FilesMatch "\.(gif|jpe?g|png|css|js|swf)$">
    Satisfy Any
</FilesMatch>

The authentication is working fine, but the SetEnvIfNoCase does not seem to work because I'm still asked for authentication when going to the friendly URL '/upload/'. The FilesMatch part does not seem to work either ..

Anyone can help me with that ?

Thanks !

  • 写回答

1条回答 默认 最新

  • dongpao5658 2014-08-04 16:18
    关注

    There's 2 things you need to address.

    1. Your <FilesMatch> container is doing nothing. You already have a Satisfy Any. You should add those extensions to a separate SetEnvIfNoCase

    2. When you are using rewrite rules that rewrite /upload/, you need to include both the "friendly" URI as well as the rewritten URI, because the auth module is applied at every step (obviously, since it's really really bad to allow bypassing of authentication by rewrite/alias). That means, for example, if you have this rule:

      RewriteRule ^upload/(.*)$ /upload_script.php?file=$1 [L]
      

    Then you need to have a NO_AUTH line for both /upload/ and /upload_script.php:

    SetEnvIfNoCase Request_URI ^/upload/ NO_AUTH
    SetEnvIfNoCase Request_URI ^/upload_script.php NO_AUTH
    

    Then to address #1:

    SetEnvIfNoCase Request_URI \.(gif|jpe?g|png|css|js|swf)$ NO_AUTH
    

    So an example of the suggestion:

    something that you can try is to add a rule (similar to the above RewriteRule example), to route the /upload/ to an intermediate script other than index.php, then from the intermediate script, call index.php.

    So using the above example of /upload_script.php you'd add this before any of your rules:

    RewriteRule ^upload/ /upload_script.php [L]
    

    So that it looks something like:

    RewriteEngine On 
    
    RewriteRule ^upload/ /upload_script.php [L]
    
    RewriteCond %{REQUEST_FILENAME} -s [OR] 
    RewriteCond %{REQUEST_FILENAME} -l [OR] 
    RewriteCond %{REQUEST_FILENAME} -d 
    RewriteRule ^.*$ - [NC,L] 
    
    RewriteRule ^.*$ index.php [NC,L]
    

    Then, you create a script called upload_script.php which will have something like:

    <?php
      include_once "index.php";
    ?>
    

    You may want to dump the $_SERVER[''] array first to see if everything's set to what Zend's index.php is expecting to see.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面