【问题标题】:How can I use htaccess to protect a subdirectory of codeigniter installation?如何使用 htaccess 保护 codeigniter 安装的子目录?
【发布时间】:2010-04-03 05:08:39
【问题描述】:

我在根目录下安装了 codeigniter,并希望有一个名为“test”的子目录使用 htaccess 保护密码。无论我尝试什么,我都会不断收到“找不到 404 页面”。目录结构为:

/public_html  
    /css  
    /images
    /system (codeigniter directory)
    /test
        .htaccess
.htaccess
.htpasswd
index.php

根 .htaccess 文件如下所示:

RewriteEngine On
RewriteBase /

Options -Indexes    

# Removes trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

# Enforce www
RewriteCond %{HTTP_HOST} !^(www) [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]


#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(.*)test(.*)    
RewriteRule ^(.*)$ index.php?/$1 [L]

/test/.htaccess 文件:

AuthUserFile /home/dir/.htpasswd
AuthName "Protected Area"
AuthType Basic
<limit GET POST PUT>
  require user adminuser
</limit>

当我导航到 URL“http://www.mydomain.com/test/”时,我什至没有收到身份验证提示,只有 codeigniter 404 页面。

请指教!

【问题讨论】:

    标签: .htaccess codeigniter basic-authentication subdirectory


    【解决方案1】:

    这是我最终解决的方法(在 CodeIgniter 论坛http://codeigniter.com/forums/viewthread/56677/P15/ 找到解决方案):

    我的根 htaccess 中有这一行:

    RewriteCond $1 !^(401.shtml)
    

    所以我的根 htaccess 中的最后一个块最终看起来像这样:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(401.shtml)
    RewriteRule ^(.*)$ index.php?/$1 [L]
    

    找到解决方案让我松了一口气。

    【讨论】:

      【解决方案2】:
      RewriteCond $1 !^(index\.php|addFoldersHere)
      

      尝试使用此方法代替您的主 .htaccess 中当前的 RewriteCond 规则。这将允许您指定的文件夹(上面写着addFoldersHere)不会被忽略。确保不要在文件夹列表之后添加尾随 |

      【讨论】:

        猜你喜欢
        • 2018-08-30
        • 1970-01-01
        • 1970-01-01
        • 2010-12-27
        • 2014-05-18
        • 2012-08-21
        • 2012-08-12
        • 1970-01-01
        相关资源
        最近更新 更多