开发者

Apache Config - Exclude Subdirectories

Is there a way to apply a rule to a single dire开发者_如何学Pythonctory and not its subdirectories without using .htaccess files? I want /var/www/html to order by the Date desc. However, in the subdirectories, I don't want this rule applied.

Right now, I have IndexOrderDefault Descending Date within the /var/www/html Directory tag.


For users who encounter this problem: See https://issues.apache.org/bugzilla/show_bug.cgi?id=49809 (Apache bug 49809). $ is not recognized by Apache as an end of line anchor for some inexplicable reason.


You can use the DirectoryMatch directive to match that directory (and only that directory) exactly:

<DirectoryMatch "^/www/var/html$">
    IndexOrderDefault Descending Date
</DirectoryMatch>

It uses regular expressions to match the directory path. See http://httpd.apache.org/docs/2.0/mod/core.html#directorymatch for more info.


Rich Leland’s answer doesn’t work for me under Apache 2.4.43. What does work is this:

<DirectoryMatch '^/var/www/html/?$'>
    IndexOrderDefault Descending Date
    </DirectoryMatch>

The difference is the /?$, which allows for requests that ‘may or may not end in a trailing slash’. See <DirectoryMatch> in the 2.4 manual.

Beware though, the above works only for directives such as IndexOrderDefault that apply exclusively to directory requests. (I think the manual is unclear on this point.) If you want to add a directive that applies to requests for regular files contained in the directory, then you need something like this:

<DirectoryMatch '^/var/www/html(?:/[^/]*)?$'>
    IndexOrderDefault Descending Date
    SetOutputFilter DEFLATE
    </DirectoryMatch>

Note that works only if all subdirectory requests are terminated with a slash, which effectively they are when <DirectorySlash> is left at its default setting.

And all of this requires Apache version 2.3.9 or later. “Prior to 2.3.9, [<DirectoryMatch>] implicitly applied to sub-directories (like <Directory>) and could not match the end of line symbol ($).”

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜