Apache的文档说:
ServerAlias可能包含通配符。
和
通配符*和? 可以用来匹配名称
我的同事声称,问号与除句点( . )以外的任何字符匹配,因此可用于“单级”通配符。 我找不到任何支持此用法的文档。
在ServerAlias指令中,问号是什么意思? 请引用文档。
你的同事是正确的。 这个? 通配符确实用于匹配单个非. 字符有效的DNS名称。
你可以在其他几个文件中提到? 字符, 如果他们描述它的使用,他们总是说一些像In a wild-card string, ? matches any single character, and * matches any sequences of characters. In a wild-card string, ? matches any single character, and * matches any sequences of characters. 不幸的是,我认为在任何地方都提到了这两种语法的含义。
问号( ? )匹配单个字符,包括句点。 用于比较主机名和ServerAlias es的函数是ap_strcasecmp_match ( server / util.c:212 )。
// server/util.c int ap_strcasecmp_match(const char *str, const char *expected) { int x, y; for (x = 0, y = 0; expected[y]; ++y, ++x) { if (!str[x] && expected[y] != '*') return -1; if (expected[y] == '*') { while (expected[++y] == '*'); if (!expected[y]) return 0; while (str[x]) { int ret; if ((ret = ap_strcasecmp_match(&str[x++], &expected[y])) != 1) return ret; } return -1; } else if (expected[y] != '?' && apr_tolower(str[x]) != apr_tolower(expected[y])) return 1; } return (str[x] != '\0'); }
假设单独testing函数是有意义的,很容易看出问号与单个字符(包括句点)相匹配。
ap_strcasecmp_match("foo.bar.com", "?.bar.com") // 1 ap_strcasecmp_match("f.bar.com", "?.bar.com") // 0 ap_strcasecmp_match("fg.bar.com", "?.bar.com") // 1 ap_strcasecmp_match("..bar.com", "?.bar.com") // 0 ap_strcasecmp_match("fgbar.com", "???.bar.com") // 0
零是一个匹配,其他的不是。