00 Votes

Expires Header without htaccess

Question by Chematik | 2012-08-19 at 20:01

I would like to put on the pages of my website an Expires Header, so that the pages can be cached.

On the Internet, I found the following lines, which you can write into the .htaccess file, to set the Expires Header of the HTML pages to a month in the future:

ExpiresActive On
ExpiresByType text/html "access plus 1 month"

Unfortunately, this is not working for me. Again, I have searched and apparently, my hosting provider prevents this manipulation.

Now, I would like to set an Expires Header, nevertheless. Are there alternatives to the solution with htaccess?

ReplyPositiveNegative
1Best Answer1 Vote

As an alternative to the htaccess solution, you can also work directly with PHP. Just write following PHP code at the beginning of each page:

header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', 
time() + 60));

The number at the back (here 60) is the time in seconds. For one month, accordingly, it would be 2592000 seconds.

It is important, that you call header() before there was any other output for the browser. Otherwise, it will give an error message.

The nice thing about this PHP solution is also, that you can specify an individual Expires Header for each page. Thus, pages that change often can get another Expires Header than pages that never change (such as your imprint or the like).
2012-08-20 at 13:11

ReplyPositive Negative
Reply

Related Topics

PHP: Sending an E-Mail

Tutorial | 0 Comments

PHP: File Download Script

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.