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

发布于 2024-08-28 02:13:47 字数 1272 浏览 4 评论 0 原文

我在根目录安装了 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/"。

请指教!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尬尬 2024-09-04 02:13:47

这是我最终解决它的方法(在 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]

所以松了一口气,解决方案就在那里。

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.

っ左 2024-09-04 02:13:47
RewriteCond $1 !^(index\.php|addFoldersHere)

尝试用此方法代替主 .htaccess 中当前的 RewriteCond 规则。这将允许您指定的文件夹(其中显示 addFoldersHere)不被忽略。确保您没有在文件夹列表后添加尾随 |

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文