环境为 Centos7.2,其他环境可能略有不同。

Apache 的 httpd.conf 中,有个必须的配置

1
2
3
4
<Directory /html/80note.com>
AllowOverride None
Require all granted
</Directory>

其中 /html/80note.com 为网站文件目录,Nonegranted 为配置参数。

AllowOverride 参数如下:

  1. None: 网站 .htaccess 文件将被完全忽略。
  2. All: 所有具有 .htaccess 作用域的指令都允许出现在 .htaccess 文件中。
  3. AuthConfig: 允许使用与认证授权相关的指令 (AuthDBMGroupFile , AuthDBMUserFile, AuthGroupFile, AuthName, AuthType, AuthUserFile, Require) 等。
  4. FileInfo: 使用控制文档类型的指令(DefaultType, ErrorDocument, ForceType, LanguagePriority, SetHandler, SetInputFilter, SetOutputFilter, mod_mime中的 Add*Remove* 指令等等)、控制文档元数据的指令(Header, RequestHeader, SetEnvIf, SetEnvIfNoCase, BrowserMatch, CookieExpires, CookieDomain, CookieStyle, CookieTracking, CookieName)、mod_rewrite中的指令(RewriteEngine, RewriteOptions, RewriteBase, RewriteCond, RewriteRule)和mod_actions中的Action指令。
  5. Indexes: 允许使用控制目录索引的指令(AddDescription, AddIcon, AddIconByEncoding, AddIconByType, DefaultIcon, DirectoryIndex, FancyIndexing, HeaderName, IndexIgnore, IndexOptions, ReadmeName 等)。
  6. Limit: 允许使用控制主机访问的指令(Allow, Deny, Order)。 一般 Apache 新安装 AllowOverride 默认为None

Wordpress 用户需要将参数设置成All,或者FileInfo,否则无法自定义网页链接。

访问控制

1. Order 指令

order指令用于指定执行允许(allow)访问控制规则或者拒绝(deny)访问控制规则的顺序。 order只能设置为Order allow,denyOrder deny,allow

  1. Order allow,deny:缺省禁止所有客户机的访问,且 Allow 语句在 Deny 语句之前被匹配。如果某条件既匹配 Deny 语句又匹配 Allow 语句,则 Deny 语句会起作用(因为 Deny 语句覆盖了 Allow 语句)。
  2. Order deny,allow:缺省允许所有客户机的访问,且 Deny 语句在 Allow 语句之前被匹配。如果某条件既匹配 Deny 语句又匹配 Allow 语句,则 Allow 语句会起作用(因为 Allow 语句覆盖了 Deny 语句)。

Allow 和 Deny 语句可以针对客户机的域名或IP地址进行设置,以决定哪些客户机能够访问服务器。

2. allow 指令

指明允许访问的地址或地址序列。如allow from all指令表明允许所有IP来的访问请求。

3. deny 指令

指明禁止访问的地址或地址序列。如deny from all指令表明禁止所有IP来的访问请求。 在下面的例子中,80note.com 域中所有主机都允许访问网站,而其他非该域中的任何主机访问都被拒绝,因为 Deny 在前,Allow 在后,Allow 语句覆盖了 Deny 语句:

1
2
3
Order Deny,Allow
Deny from all
Allow from 80note.com

下面例子中,80note.com 域中所有主机,除了db.80note.com 子域包含的主机被拒绝访问以外,都允许访问。而所有不在 80note.com 域中的主机都不允许访问,因为缺省状态是拒绝对服务器的访问(Allow在前,Deny在后,Deny语句覆盖了Allow语句):

1
2
3
Order Allow,Deny
Allow from 80note.com
Deny from db.80note.com

4. Require  指令

Require all granted 无条件允许访问。

Require all denied 无条件拒绝访问。

Require全部参数:https://httpd.apache.org/docs/2.4/mod/mod_authz_core.html#require