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.

  1. 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 an FTP client to download it to your local computer, and then add the below code to it. But first, you should make sure the mod_headers.c apache module has been enabled in your server.
  2. 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 functional because the WordPress plugin can modify headers value in plugin PHP source code. So sometimes we need to modify the 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 the left navigation panel.
  3. Select your WordPress website used Theme.
  4. Then select the header.php file in the Theme Files list.
  5. Add below PHP source code at the very beginning of header.php file before <!DOCTYPE html>. The below source code uses the 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.

Besides the header.php file, there is also a functions.php file in WordPress theme template files. You can define and add a customized 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 source code at the end of 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 providers such as Ezoic CDN provide functions to let you configure which HTTP header you want to remove from the original server. Because CDN proxy servers can access all your web page data, so they can modify the HTTP headers as needed. Below are the steps, but first, you should register an Ezoic account to use their Caching app.

  1. Login to the Ezoic admin website.
  2. Select a domain, click the Speed icon on the top area.
  3. Scroll down the page to the EZOIC CLOUD CACHING section.
  4. Click the SETTINGS button at the end of the EZOIC CLOUD CACHING section, then it will show the Ezoic CACHING page.
  5. Scroll down the page to the Cache Setting Values area.
  6. Click the 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 the HTTP Vary header. It can also ignore the Cache-Control header.

1 thought on “How To Modify Http Response Header In WordPress”

Leave a Comment

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.