How to add security headers via .htaccess
If your site runs on Apache or LiteSpeed, the easiest way to add security headers is through the .htaccess file. The headers tell the browser how to handle your site safely: use HTTPS only, block clickjacking and load scripts only from sources you allow. Setting them up takes a few minutes and can raise your security grade to A+.
Check your site
-
Open .htaccess in the site root
In cPanel open File Manager, turn on hidden files (Settings, then Show Hidden Files) and open .htaccess inside public_html. If the file is missing, create it with New File.
-
Enable HSTS
Add the line Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains". It makes the browser always use HTTPS for the site. Turn it on only after the whole site and all its subdomains already work over HTTPS.
-
Set X-Content-Type-Options and Referrer-Policy
Add Header always set X-Content-Type-Options "nosniff" and Header always set Referrer-Policy "strict-origin-when-cross-origin". The first stops the browser from guessing file types; the second limits how much referrer data is sent to other sites.
-
Add clickjacking protection
The line Header always set X-Frame-Options "SAMEORIGIN" prevents other sites from embedding yours in an iframe. For modern browsers, repeat the rule in CSP with the frame-ancestors 'self' directive.
-
Set Content-Security-Policy carefully
Start with a basic policy such as Header always set Content-Security-Policy "default-src 'self'; img-src 'self' data:; script-src 'self'". It blocks inline scripts and styles and everything loaded from other sites, so first list every source the site really loads from: if you forget one external script or font, CSP can break the layout. You can try the policy first in Report-Only mode, which reports violations without blocking them.
-
Wrap the directives in <IfModule mod_headers.c>
On a server without mod_headers the Header lines cause a 500 error, so wrap the block in <IfModule mod_headers.c> and </IfModule>. Typical LiteSpeed hosting has the module enabled, but the wrapper costs nothing.
-
Save the file and clear the cache
Save .htaccess and purge LiteSpeed Cache or any other cache the site uses. The headers apply immediately, with no server restart.
How to verify the result
Open the site in an incognito window and check the response headers in the browser's Network tab, or run your domain through the SpoofLens security headers checker. It gives a grade from A+ to F and lists the headers that are still missing.
Tip: Do not add the preload directive to HSTS until HTTPS works across the entire domain: rolling preload back is slow and painful.