How to implement Security Headers for Apache Web Server (htaccess) Website

Sitebee

Technical SEO
Staff member
Have you been reading into HTTP website security headers and looking to implement the security header for your website? I will show you a quick and easy method via your HTACCESS file.

By security headers I am referring to the following headers that not commonly set by your web hosting provider or your Content Management System (CMS) platform such as Wordpress, Magento, etc.

Strict-Transport-SecurityHTTP Strict Transport Security is an excellent feature to support on your site and strengthens your implementation of TLS by getting the User-Agent to enforce the use of HTTPS. Recommended value “Strict-Transport-Security: max-age=31536000; includeSubDomains”.
Content-Security-PolicyContent Security Policy is an effective measure to protect your site from XSS attacks. By whitelisting sources of approved content, you can prevent the browser from loading malicious assets.
X-Frame-OptionsX-Frame-Options tells the browser whether you want to allow your site to be framed or not. By preventing a browser from framing your site you can defend against attacks like clickjacking. Recommended value “X-Frame-Options: SAMEORIGIN”.
X-Content-Type-OptionsX-Content-Type-Options stops a browser from trying to MIME-sniff the content type and forces it to stick with the declared content-type. The only valid value for this header is “X-Content-Type-Options: nosniff”.
Referrer-PolicyReferrer Policy is a new header that allows a site to control how much information the browser includes navigations away from a document and should be set by all sites.
Feature-PolicyFeature Policy is a new header that allows a site to control which features and APIs can be used in the browser.


Firstly, we need to know what security headers we need to implement. For this, we are going to run an HTTP security header scan over at https://securityheaders.com

I’m running this test here on my website ‘Sitebee’ as I know it was on my to-do list and thought it would be an excellent resource for a blog post.

Here is the security header benchmark before implementing the HTTP security headers.
sitebee-f.png


Now here are the same benchmarks after implementation of the HTTP security headers.
sitebee-a.png


Add the following security header HTACCESS directives to your HTACCESS file.

Code:
# Strict-Transport-Security
Header set Strict-Transport-Security “max-age=31536000” env=HTTPS

# X-Content-Type-Options
Header set X-Content-Type-Options “nosniff”

# X-XSS-Protection
Header set X-XSS-Protection “1; mode=block”

# Referrer-Policy
Header set Referrer-Policy “same-origin”

# Feature-Policy
Header set Feature-Policy “geolocation ‘self’; vibrate ‘none”

# X-Frame-Options
Header always append X-Frame-Options SAMEORIGIN
 

Attachments

  • sitebee-f.png
    sitebee-f.png
    17.5 KB · Views: 72