在 apache 中,如何验证登录的用户名是否与目录名匹配?

在 apache 中,如何验证登录的用户名是否与目录名匹配?

有时您想在 apache 中设置一个目录结构,以便登录的用户只能看到他们自己的目录。(即目录名和用户名匹配。)

您在配置中输入了什么以便 apache 可以检查这一点?

答案1

因此,这个想法是,人们只需浏览/directory/并输入他们的用户名和密码。然后,/directory/username/如果他们试图进入其他人的目录,系统会将他们重定向到并检查目录和用户名是否匹配。

AuthType Basic
AuthName "Your Realm Name"
Order allow,deny
AuthUserFile .......
Require valid-user

RewriteEngine On
#If just to /directory then redirect to the per user area
RewriteRule ^$ /directory/%{REMOTE_USER} [R,L]
#WARNING: make sure usernames can't have slashes in them
#grab the directory from the URL from RewriteRule (it's called $1)
#use a REGEX backreference to see if $1 equals %{REMOTE_USER} with a slash in between for performance
RewriteCond $1/%{REMOTE_USER} !^([^/]+)/\1$
#If they don't match then give them a fail page
RewriteRule ^([^/]+) - [F]

相关内容