如何通过“files”指令选择子目录中的文件?

问题描述:

我想编译特定的.htm文件的PHP代码,如果位于域的根目录下的文件,我可以添加如何通过“files”指令选择子目录中的文件?

<Files file.htm> 
    AddType application/x-httpd-php5 .htm 
</Files> 

为.htaccess,但我应该怎么做,如果文件位于另一个目录(/分/ file.htm,例如)?

尝试FilesMatch:

<FilesMatch "file.htm$"> 
    AddType ... 
</Files> 

它适用于正则表达式的文件名,所以这将匹配具有“file.htm”结尾的任何文件路径。它适用于以“file.html”结尾的任何目录中的任何文件。请注意,这也会匹配“somefile.htm”和“/subdir/somefile.htm”。如果你的站点树中有其他文件可能触发这个问题,你必须修改正则表达式来排除它们。

+0

我可以在正则表达式中以某种方式使用目录名称来选择正确的文件吗? – Anatoly

+0

当然。文件匹配运算符在文件的文件系统路径上(而不是请求的url,但实际上apache在与服务器的文件系统交谈时考虑的是什么),并且可以包括目录。 –

+0

' \/sub \ /file.htm“> AddType应用程序/ x-httpd-php5 .htm '是吗? – Anatoly

也许你想使用httpd.conf而不是.htaccess文件。然后,您可以在这种情况下使用<Location>-directive

<Location /sub/file.htm> 
    AddType application/x-httpd-php5 .htm 
</Location> 

注意,如果你想使用正则表达式匹配的位置,你需要写的位置之前~或使用<LocationMatch>-directive

例如:

<Location ~ "/sub/(file|anotherfile|andanother).htm"> 
    AddType application/x-httpd-php5 .htm 
</Location> 

编辑:

另一种可能的解决方案(也仅在虚拟主机-CONFIG)应该是一个结合<Directory><Files> -directive:

<Directory /sub> 
    <Files file.htm> 
    AddType application/x-httpd-php5 .htm 
    </Files> 
</Directory> 
+0

因为它,我得到“500内部服务器错误”。尝试这样: 将AddType应用/ X的httpd - PHP5的.htm LocationMatch> 将AddType应用/ X的httpd - PHP5的.htm LocationMatch> – Anatoly

+0

请求URL是domain.com/sub/file.htm – Anatoly

+0

' AddType应用程序/ x-httpd-php5。htm '也得到“500内部服务器错误”。 – Anatoly

也许如果您使用的httpd 2.4具有<if> supp,那么可以这样做orts

<If "%{PATH_INFO} =~ /.*\/sub\/(file|anotherfile|andanother)\.htm/"> 
    AddType application/x-httpd-php5 .htm 
</If>