我有一个虚拟目录结构的网站,例如mysite.com/folder/title
,实际上是将.htaccess重写为mysite.com/f/index.php?p=title
。我想用.htaccess密码保护文件夹folder
,并知道如何对实际文件夹进行保护。但是我不想用密码保护主站点mysite.com
,现在,如果我将.htaccess文件放在mysite.com
目录中,则表示正在保护mysite.com
和mysite.com/folder
。我也尝试过保护mysite.com/f
。
如何使用.htaccess保护仅mysite.com/folder
?
编辑:添加了mysite.com
的.htaccess内容。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^folder/(.*)$ /f/index.php?p=$1 [PT,L,QSA]
RewriteRule ^folder/*$ /f/index.php [L,QSA]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
我在
mysite.com/f
中尝试过的.htaccess文件当移至mysite.com
时,此文件成功地保护了整个站点,因此我知道路径是正确的。当它在子目录中时,它将生成404错误,而不是密码提示。AuthName "Restricted Area"
AuthType Basic
AuthUserFile /home/myusername/.htpasswd
require valid-user
最佳答案
旧线程很旧...
在遇到类似问题时偶然发现了这个问题,密码保护了子域,而主站点却没有。
该解决方案比我最初设想的要容易。
在document_root / .htaccess中,domain.com/wiki被重定向到domain.com/w(因为这样更干净?大声笑):
RewriteEngine On
RewriteRule ^/?w(/.*)?$ /wiki/index.php [PT,L,QSA]
RewriteRule ^/*$ /wiki/index.php [L,QSA]
在document_root / wiki / .htaccess中,Wiki目录受密码保护:
AuthType Basic
AuthName "Restricted"
AuthUserFile "/home/user/.htpasswds/public_html/wiki/passwd"
require valid-user
我只是将此行添加到document_root / .htaccess的顶部,因此它显示为:
AuthType None
RewriteEngine On
RewriteRule ^/?w(/.*)?$ /wiki/index.php [PT,L,QSA]
RewriteRule ^/*$ /wiki/index.php [L,QSA]
domain.com不再受密码保护,domain.com / wiki会按预期并使用密码保护进行重定向。
希望它可以帮助其他人。
关于.htaccess - 使用.htaccess密码保护虚拟目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3670966/