代码之家  ›  专栏  ›  技术社区  ›  Art Peterson

如何使用htaccess保护codeigniter安装的子目录?

  •  0
  • Art Peterson  · 技术社区  · 15 年前

    我在根目录下安装了codeigniter,并希望使用htaccess来保护名为“test”的子目录。不管我怎么尝试,我总是得到一个“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时的codeigniter 404页面。” http://www.mydomain.com/test/ “。

    请告知!

    2 回复  |  直到 15 年前
        1
  •  2
  •   Art Peterson    15 年前

    以下是我最终解决问题的方法(在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
  •  0
  •   stormdrain    15 年前
    RewriteCond $1 !^(index\.php|addFoldersHere)
    

    在main.htaccess中尝试此操作,以代替当前的重写条件规则。这将允许您指定的文件夹(其中显示 addFoldersHere )不可忽视。确保不添加尾随 | 在文件夹列表之后。