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.
- 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.
- 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.
- Login to your WordPress admin website.
- Click Appearance —> ( Theme ) Editor menu item in the left navigation panel.
- Select your WordPress website used Theme.
- Then select the header.php file in the Theme Files list.
- 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.
- 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.
- Login to the Ezoic admin website.
- Select a domain, click the Speed icon on the top area.
- Scroll down the page to the EZOIC CLOUD CACHING section.
- Click the SETTINGS button at the end of the EZOIC CLOUD CACHING section, then it will show the Ezoic CACHING page.
- Scroll down the page to the Cache Setting Values area.
- 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.
Nice article. How would you recommend to set up Same Site cookie header? Would .htaccess method be ok?