简体   繁体   中英

How can I use htaccess to protect a subdirectory of codeigniter installation?

I have codeigniter installed at the root directory, and would like to have a subdirectory called "test" password protected using htaccess. I keep getting a "404 page not found" no matter what I try. The directory structure is:

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

The root .htaccess file looks like:

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]

The /test/.htaccess file:

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

I'm not even getting the authentication prompt, just the codeigniter 404 page when I navigate to the url " http://www.mydomain.com/test/ ".

Please advise!

Here is how I finally solved it (found solution in CodeIgniter forum at http://codeigniter.com/forums/viewthread/56677/P15/ ):

I had this line to my root htaccess:

RewriteCond $1 !^(401.shtml)

So the final block in my root htaccess ended up looking like this:

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

So relieved a solution was out there.

RewriteCond $1 !^(index\.php|addFoldersHere)

Try this in place of your current RewriteCond rule in your main .htaccess. This will allow folders you specify (where it says addFoldersHere ) to not be ignored. Make sure you don't add a trailing | after the list of folder(s).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM