-2

I would like to send no-cache headers if user is admin like current_user_can('administrator')

So browser will not cache some resurces. (In order to prevent ctrl + f5 everytime)

Near solutions also welcome.

During seaching I found that

RewriteCond %{HTTP:Cookie} !(wordpress_logged_in_|wp-postpass_) [NC]

is it possible to send no-cache header with above approach?

Deniz Porsuk
  • 492
  • 1
  • 6
  • 20
  • Sure just send that header with `header`. What doss this have to do with .htaccess? – Jonathon Reinhart Jul 22 '15 at 09:56
  • What you mean by send header with header? – Deniz Porsuk Jul 22 '15 at 10:05
  • About your edit, there is nothing in the browser agent string that can tell you if the user is logged in or not, and futhermore his role. – vard Jul 22 '15 at 10:07
  • Things written in `code format` generally refer to code. One second of research would point you to the PHP [`header`](http://php.net/manual/en/function.header.php) function. – Jonathon Reinhart Jul 22 '15 at 10:08
  • @JonathonReinhart If you read my question before answer. You will see that I'm asking I would like to send no-cache headers if user is admin. I'm not asking how to send headers. I will not answer to you anymore. – Deniz Porsuk Jul 22 '15 at 10:15
  • Are you trolling right now? You would use that function to send the header you're asking about. It does precisely what you ask. Getting mad at people who know what they're talking about won't get you very far in life. – Jonathon Reinhart Jul 22 '15 at 10:17

2 Answers2

0

simply, you can put the admin area in a separate folder other than the folder of the public website and set cache for that folder as follows:

<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
<FilesMatch "\.(css|js)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

max-age = time in seconds .. set it to 1800 or something to last for 30 mins

other than that, no, htaccess can't check if a "user" is admin or not, you have to do it with php

Ehab Eldeeb
  • 722
  • 4
  • 12
0

htaccess can't access sessions, so no you can't check if the user is logged or whatever in it. But as said before, you can check it in PHP, and as you use Wordpress, you could put this in your functions.php :

function admin_nocache() {
    if(is_admin()) {
        nocache_headers();
    }
}
add_action( 'init', 'admin_nocache' );

See https://codex.wordpress.org/Function_Reference/nocache_headers

vard
  • 4,057
  • 2
  • 26
  • 46
  • This is good idea. Is it possible to overwrite .htaccess headers from php? – Deniz Porsuk Jul 22 '15 at 10:12
  • headers can be changed as long they're not sent. headers are sent when the first content is displayed - or when the script exit, so you can change them as much as you want until this happen. – vard Jul 22 '15 at 10:18