开发者

Password Protect Virtual Directory With .htaccess

I have a site with a virtual directory structure like mysite.com/folder/title which is actually a .htaccess rewrite to mysite.com/f/index.php?p=title. I want to password protect the folder folder with .htaccess, and know how to do that with actual folders. But I don't want to password protect the main site mysite.com, and right now if I put the .开发者_运维百科htaccess file in the mysite.com directory, I am protecting mysite.com and mysite.com/folder. I have also tried protecting mysite.com/f.

How can I protect only mysite.com/folder using .htaccess?

EDIT: Added .htaccess contents of mysite.com.

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

.htaccess file I tried in mysite.com/f This successfully protects the entire site when moved to mysite.com, so I know the path is correct. When it is in the subdirectory it generates a 404 error and not a password prompt.

AuthName "Restricted Area" 
AuthType Basic 
AuthUserFile /home/myusername/.htpasswd 
require valid-user


Old thread is old...

Stumbled across this while having a similar issue, password protecting a subdomain while keeping the main site without.

The solution was easier than I originally made it out to be.

In the document_root/.htaccess, domain.com/wiki was redirecting to domain.com/w (because that's cleaner? lol):

RewriteEngine On
RewriteRule ^/?w(/.*)?$ /wiki/index.php [PT,L,QSA]
RewriteRule ^/*$ /wiki/index.php [L,QSA]

In document_root/wiki/.htaccess the wiki directory was password protected:

AuthType Basic
AuthName "Restricted"
AuthUserFile "/home/user/.htpasswds/public_html/wiki/passwd"
require valid-user

I simply added this line to the top of document_root/.htaccess so it reads:

AuthType None
RewriteEngine On
RewriteRule ^/?w(/.*)?$ /wiki/index.php [PT,L,QSA]
RewriteRule ^/*$ /wiki/index.php [L,QSA]

domain.com is no longer password protected and domain.com/wiki redirects as intended and with password protection.

Hope it helps someone else.


One thing that works (but isn't the most elegant solution) is to actually create a folder named "folder" (or whichever virtual folder you're trying to password-protect) and put the .htaccess into it.


Assuming you are using Apache 2.2

... and, that your server is running on a machine to which you do not have permission to modify the server configuration

... then, create: mysite.com/f/.htaccess with the rule you desire.

A good discussion of when to use this and when not to can be found here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜