2011-11-03 46 views
0

我想編譯特定的.htm文件的PHP代碼,如果位於域的根目錄下的文件,我可以添加如何通過「files」指令選擇子目錄中的文件?

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

爲.htaccess,但我應該怎麼做,如果文件位於另一個目錄(/分/ file.htm,例如)?

回答

0

嘗試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

0

也許你想使用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 將AddType應用/ X的httpd - PHP5的.htm Anatoly

+0

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

+0

'<指南/子> <文件file.htm> AddType應用程序/ x-httpd-php5。htm '也得到「500內部服務器錯誤」。 – Anatoly

1

也許如果您使用的httpd 2.4具有<if> supp,那麼可以這樣做orts

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