Production Security Headers & .htaccess Hardening Guide
Ship production-grade HTTP security headers (CSP, HSTS, X-Content-Type-Options, Referrer-Policy, Permissions-Policy) and hardened Apache .htaccess rules blocking .env, .git, and xmlrpc.php.
// key takeaways
- Roll out Content-Security-Policy in Content-Security-Policy-Report-Only mode first so you catch broken third-party scripts before enforcing strict nonce or hash rules.
- Pair HSTS (Strict-Transport-Security) with a permanent 301 HTTPS redirect, and only add includeSubDomains + preload once every subdomain reliably serves valid TLS.
- On Apache, PHP, and WordPress hosts, explicitly deny access to dotfiles (.env, .git), backup archives (.sql, .bak), directory listings, and xmlrpc.php at the web-server layer.
- Verify live response headers automatically in CI with curl -sI so CDN or proxy rewrites never silently strip security headers.
The 2026 baseline for production HTTP security headers
Security headers act as defense-in-depth inside the user's browser: even if an attacker finds an injection flaw or injects a third-party dependency, strict browser policies constrain script execution, frame embedding, protocol downgrades, and sensitive API permissions.
Start with a clean, low-friction baseline across every HTML route, then tighten Content-Security-Policy iteratively until unsafe-inline and unsafe-eval are completely eliminated from script-src:
- Strict-Transport-Security: max-age=31536000; includeSubDomains (test with max-age=300 first before committing to a full year or preload).
- Content-Security-Policy: Lock default-src 'self', restrict script-src with nonces or SHA-256/384 hashes, set object-src 'none', base-uri 'self', and frame-ancestors 'none' (replaces legacy X-Frame-Options).
- X-Content-Type-Options: nosniff — stops browsers from MIME-sniffing user uploads or plain text into executable JavaScript.
- Referrer-Policy: strict-origin-when-cross-origin — prevents sensitive path tokens and reset URLs from leaking in cross-origin Referer headers.
- Permissions-Policy: Explicitly disable unused hardware APIs (camera=(), microphone=(), geolocation=(), payment=()) so compromised third-party widgets cannot abuse them.
- Cross-Origin-Opener-Policy: same-origin — isolates your browsing context group from cross-origin window openers.
Rolling out a strict Content-Security-Policy without breaking production
The fastest way to make engineers hate CSP is shipping a strict policy straight to enforcement on a live app and breaking checkout widgets, analytics, or inline hydration scripts. Always deploy CSP in two phases.
First, ship Content-Security-Policy-Report-Only with a reporting endpoint (Reporting-Endpoints / report-to) and monitor violations across real user sessions for at least one full release cycle. Second, replace inline script blocks with per-request cryptographic nonces or static SHA-384 hashes (plus Subresource Integrity attributes on external CDN assets), then flip to enforcing Content-Security-Policy.
Hardening Apache & WordPress servers with .htaccess
Automated scanners continuously sweep Apache and PHP hosts for exposed .env files, .git/HEAD metadata, database dumps, phpinfo scripts, and WordPress xmlrpc.php brute-force endpoints. Blocking these at the web server layer stops disclosure even if an accidental deployment drops a sensitive file inside the web root.
# Disable directory browsing and server signature leakage
Options -Indexes
ServerSignature Off
# Force HTTPS + canonical host
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Block dotfiles (.env, .git, .htaccess) except ACME challenges
RewriteRule (^|/)\.(?!well-known/) - [F,L]
# Block sensitive backup/config extensions and WordPress xmlrpc.php
<FilesMatch "(?i)(\.(bak|config|dist|env|ini|log|sh|sql|swp)|^xmlrpc\.php)$">
Require all denied
</FilesMatch>
# Core security headers
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
Header always set Content-Security-Policy "default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'"
</IfModule>Pair headers with least-privilege file permissions and CI smoke checks
Web-server rules work best alongside clean Unix file permissions: keep web directories at 755, static files at 644, configuration secrets like .env or wp-config.php at 600/640 owned by the deploy user, and never run chmod 777 on upload folders.
Finally, add a lightweight smoke check to your deploy workflow that runs curl -sI against your production URL and asserts that HSTS, CSP, nosniff, and a 403/404 on /.env and /.git/HEAD remain intact after every CDN or framework upgrade.
// actionable engineering checklist
Test CSP in Report-Only mode before enforcing
Capture real script/style/connect violations and replace unsafe-inline with nonces or SHA-384 hashes.
Block dotfiles, backup archives, and xmlrpc.php at the web server
Verify /.env, /.git/HEAD, and /xmlrpc.php return 403 or 404 in production.
Audit Linux web-root file modes (755 dirs, 644 files, 600 secrets)
Eliminate world-writable directories and keep secret config files outside the public document root whenever possible.
Assert security headers automatically on every deploy
Catch accidental proxy or Cloudflare header rewrites in CI before an external audit flags them.
// connected pwn.watch tools & live trackers
.htaccess Security Generator →
Generate clean Apache HTTPS redirects, dotfile/xmlrpc blocks, caching, compression, and security headers.
CSP & SRI Header Generator →
Build Content-Security-Policy directives and SHA-384 Subresource Integrity hashes locally.
Linux Chmod Calculator →
Calculate exact 755 / 644 / 600 octal and symbolic file permissions.
Stack & Tag CVE Trackers
Security Hubs & Feeds
Frequently Asked Questions
- Is X-XSS-Protection still needed in 2026?
- No. Modern browsers removed legacy XSS auditors because they could introduce XS-Leaks. Rely on a strong Content-Security-Policy and contextual output encoding instead.
- Why should I be careful before enabling HSTS preload?
- Once your domain is submitted to browser HSTS preload lists, every subdomain — including legacy staging or internal tools — must serve valid HTTPS. Removing a domain from preload lists takes months.
- Does .htaccess work on Nginx or Cloudflare Workers?
- No. .htaccess is specific to Apache HTTP Server (and LiteSpeed). On Nginx configure location and add_header blocks in nginx.conf; on Cloudflare Pages/Workers use a public/_headers file or edge middleware.
Get an email when a new major security incident drops. No spam, unsubscribe any time.