Set HTTP header for all PHP scripts via .htaccess

2020-06-17 14:39发布

问题:

I code same lines all PHP programs at one of my projects. Is it possible to do this at .htaccess for a directory? And how?

PHP codes:

Header('Content-Type: application/xhtml+xml; charset=utf-8');
Header("Cache-Control: no-transform");

Thanks any help. Best regards.

Yusuf Akyol

回答1:

If you want to do this with .htaccess (or in Apache config), you can use Apache module mod_headers, like this:

Header set Cache-Control "no-transform"
Header set Content-Type "application/xhtml+xml; charset=utf-8"

A search on htaccess set header gives you many more examples of this.



回答2:

For the content type you use AddType from mod_mime. Assuming all your files have .php extensions:

AddType application/xhtml+xml .php

Caching is set in mod_expires, but you need mod_headers to set Cache-Control:

<FilesMatch "\.php$">
    Header set Cache-Control "no-transform"
</FilesMatch>