I am trying to add support for Vary: Accept-Encoding from whithin my theme’s function.php file.

Please don’t suggest me to use any plugin, because that the only reason why I am trying to implement this from within my theme.

The idea of using insert_with_markers from this StackOverflow discussion: How to add .htaccess code through a function?

Can you please tell me what I have done wrong?

function add_htaccess($insertion)
    $insertion = array(
        '# Enable Vary: Accept-Encoding Start',
        '<IfModule mod_headers.c>',
        '<FilesMatch "\.(js|css|xml|gz)$">',
        'Header append Vary: Accept-Encoding',
    $htaccess_file = ABSPATH.'.htaccess';
    return insert_with_markers($htaccess_file, '# Testing', (array) $insertion);
  • you're trying to do this in your theme to avoid adding another plugin? But the code would be exactly the same? Can you explain why you're doing this from the theme rather than just modifying .htaccess yourself? What about those using Nginx, lighttpd or cloud services that don't rely on Apache?
    – Tom J Nowell
    Jul 31, 2017 at 13:25
  • It's Apache server and it's localhost. I will add few conditionals also. You see my friend don't want to see "too" many plugins on her dashboard. I explained exactly what you are saying, plugin will do exactly the same thing, but she is just not ready to listen it.
    – John Adam
    Jul 31, 2017 at 15:25
  • If she's your friend and you're doing her a favour she should be glad you're helping, rather than chastise you because she heard a rumour/myth. Don't bend over backwards for superstitious nonsense if it's going to occupy your time unless you're willing to charge her for for her indulgence
    – Tom J Nowell
    Jul 31, 2017 at 15:48
  • That friend is a manager at the local webdev company. That's why I am doing it for her.
    – John Adam
    Jul 31, 2017 at 15:50

1 Answer 1


The code below has work for me in the past :)

function add_to_htaccess( $rules ) {

  $content = <<<EOD
  # Enable Vary: Accept-Encoding Start
  <IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
  Header append Vary: Accept-Encoding
 return $content . $rules;

add_filter('mod_rewrite_rules', 'add_to_htaccess');
  • No bro, I think you missed something to paste. I am getting this error "Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)"
    – John Adam
    Jul 31, 2017 at 15:33
  • Ok I made to work, problem was because of blank spaces. But this function is not updating the .htaccess file.
    – John Adam
    Jul 31, 2017 at 15:49
  • you may need to flush it - disable and enable the theme Jul 31, 2017 at 15:49
  • I did it brother. Can you show me the screenshot of the function?
    – John Adam
    Jul 31, 2017 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.