簡體   English   中英

如何在.htaccess文件中設置一些PHP標頭

[英]How to set some PHP headers in a .htaccess file

所以我在一些PHP文件中有這些標題:

<?php
    header('Content-Type: text/html; charset=UTF-8');
    header('Content-Style-Type: text/css');
    header('Content-Script-Type: application/javascript');
    header('HTTP/1.1 200 OK');
    header('Content-language: en-US');
    header('X-Powered-By: PHP/5.2.17');
    header('Last-Modified: Tue, 01 Jan 2013 00:00:00 GMT');
    header('Cache-Control: no-store, no-cache, max-age=0, must-revalidate');
    header('Pragma: no-store, no-cache, max-age=0, must-revalidate');
    header('Expires: Tue, 01 Jan 2013 00:00:00 GMT');
?>

如何在我的.htaccess文件中設置它們? (應該只針對特定文件),這是我到目前為止所得到的:

<FilesMatch "^(index.php|about.php|contact.php)$">
    # HTTP Headers should be set in here
</FilesMatch>

您需要安裝mod_headers,否則可能會收到錯誤500.您可以在條件中包裝標頭設置以檢查mod_headers是否存在。

<FilesMatch "\.(html|htm|js|css)$">
    <ifModule mod_headers.c>
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
        Header set Expires "Thu, 1 Jan 2015 05:00:00 GMT"
    </ifModule>
</FilesMatch>

您也可以取消設置標題。 例如,如果您在加載balacer后面,真實的訪問者IP可以以您不希望向應用程序公開的自定義標頭的形式傳遞。

Header unset Real-Visitor-IP

除了expiration和etags以及特定於Web服務器的自定義標頭之外,您不應該在Web服務器級別篡改標頭。 其中大多數應該在應用程序級別進行管理,如果您傳遞更改的內容類型,則會阻止某些框架正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM