沃梦达 / 编程技术 / 服务器 / 正文

使用Apache .htaccess 进行访问保护

Apache Server下,可以对单个目录进行访问控制,如ip过滤,要求用户名密码进行Basic Auth等。下面使用 .htaccess来实现Auth.首先在配置文件 http.conf里,对所要控制的目录,开启Override功能,如果在这里设置为Allo...

Apache Server下,可以对单个目录进行访问控制,如ip过滤,要求用户名密码进行Basic Auth等。下面使用 .htaccess来实现Auth.

首先在配置文件 http.conf里,对所要控制的目录,开启Override功能,如果在这里设置为AllowOverride none, .htaccess文件将不起作用。

<Directory "/your/site/dir/need/to/control/">
    Options FollowSymLinks Multiviews
    MultiviewsMatch any
    AllowOverride all
    Require all granted
</Directory>

上面的AllowOverride可以选 FileInfo, AuthConfig, Limit, all, none.

配置文件保存后,重启server. 我在mac os x Yosemite下执行

            sudo apachectl restart

Apache的配置文件到此完成,现在在对应的site目录下创建.htaccess文件,如上面的/your/site/dir/need/to/control/.htaccess,内容如下

AuthName name
AuthUserFile /your/site/dir/need/to/control/.htpasswd
AuthType Basic
require valid-user

上面的name为用户login时的用户名,AuthUserFile为存放password的文件。下面讲如何生成.htpasswd文件:

在console(mac os x Yosemite)下使用htpasswd命令生成.htpasswd文件

htpasswd -bc /your/site/dir/need/to/control/.htpasswd name password

现在在浏览器访问 http://host/your/.../control/,会弹出对话框,输入name, password即可。

 

原文:http://www.cnblogs.com/liseamas/p/4374507.html

本文标题为:使用Apache .htaccess 进行访问保护

基础教程推荐