How To Modify Http Response Header In WordPress

This article will tell you three methods about how to add, delete, override http response headers in WordPress.

1. Modify Http Response Header In .htaccess File.

wordpress website public_html directory

The .htaccess file is located in your domain’s home directory, the domain home directory is something like /home/domain_name/public_html. So you can use a ftp client to download it to your local computer, and then add below code in it. But first you should make sure the mod_headers.c apache module has been enabled in your server.

You can pass a lot of parameters to the Header command. Such as unset, set etc.

# Remove vary header to avoid ezoic cache do not take effect. 2019/03/16 
<IfModule mod_headers.c>
     # unset http header Vary
     Header unset Vary
   
     # set http header Vary's value to "Accept-Encoding" only.
     Header set Vary "Accept-Encoding"
</IfModule>

2. Modify Http Response Header In WordPress Theme Template Source Code.

But above method is not always funtional because wordpress plugin can modify headers value in plugin php source code. So sometimes we need to modify http response header value in WordPress source code.

2.1 Modify Http Response Headers In header.php File.

  1. Login to your WordPress admin website.
  2. Click Appearance —> ( Theme ) Editor menu item in left navigation panel.
    wordpress appearance menu item
  3. Select your WordPress website used Theme.
  4. Then select header.php file.
    wordpress theme header php file
  5. Add below php source code at the very beginning of header.php file before <!DOCTYPE html>. Below source code use php header function to set related header values.
    <?php
    // Replace wordpress Vary headers which value is Accept-Encoding,User-Agent,X-APP-JSON
    
    // unset http vary header.
    unset($headers['Vary']);
    
    // use php header function to set new http vary header value, 
    // for the second parameter, true means replace the previous header, false means add a second header.
    header('Vary:Accept-Encoding', true);
    ?>
    
    <!DOCTYPE html>
    <html <?php language_attributes( 'html' ); ?>>
    
    <head>
    ......

2.2 Modify Http Response Headers In functions.php File.

Beside header.php file, there is also a functions.php file in WordPress theme template files. You can define and  add a customize filter function in functions.php to replace the http response headers.

  1. Locate and edit the functions.php file in WordPress admin website like section 2.1, then add below php soruce code at the end of functions.php file.
    wordpress theme functions php file

    function replace_wp_headers($headers) {
      $headers['Vary'] = 'Accept-Encoding';
      return $headers;
    }
    
    add_filter('wp_headers', replace_wp_headers);

3. Remove Http Headers In A CDN Proxy.

Some cdn service provider such as ezoic cdn provide functions to let you configure which http header you want to remove from original server. Because cdn proxy server can access all your web page data, so they can modify the http headers as needed. Below are steps, but first you should register an ezoic account to use their Caching app.

  1. Login to ezoic admin website.
  2. Click Caching app button.
  3. Click Update Cache Setting Values button and input the headers you want to remove in Vary Headers to be removed from origin input text box.  But ezoic can only remove values in http Vary header. It can also ignore the Cache-Control header.
    ezoic caching app settings
READ :   How To Verify If CloudFlare CDN Cached Your Website Html Content, CSS, Images, JS Files

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.