Securing a WordPress site
Most WordPress fixes live in two places: the .htaccess file in your site's root folder (headers, HTTPS, blocking private files) and your WordPress settings. Start with updates, then add the headers block and the HTTPS redirect below.
Last updated October 26, 2026
Prefer to fill in a form? Use the free security headers generator or the SPF and DMARC record generator.
Not sure whether your site has this?
Paste your address. The checkup is free and shows what's flagged.
What our checkup looks for
Our free checkup reads your site's headers and cookies, checks the HTTPS redirect and mixed content, asks for files that should never be public, checks your domain's email records, and looks at what WordPress shows the public.
Every security header in .htaccess
Open the .htaccess file in your WordPress root folder through your host's file manager or FTP, and back it up first. Paste this block near the top, outside the # BEGIN WordPress section, which WordPress rewrites. The Content Security Policy starts in report-only mode, so it changes nothing for visitors yet.
If your host runs Nginx or doesn't allow .htaccess (some managed WordPress hosts don't), ask them to add these headers, or use a headers plugin.
How to fix it on WordPress (Apache .htaccess)
- Open the .htaccess file in your WordPress root folder (via your host's file manager or FTP). Make a backup copy first.
- Paste the block below near the top, outside the '# BEGIN WordPress' section, and save.
- If your host runs Nginx instead of Apache, or doesn't allow .htaccess, a plugin like 'Headers Security Advanced & HSTS WP' can set these instead.
- The Content-Security-Policy-Report-Only line changes nothing for visitors yet. After a week with no broken features (check your browser's console), rename it to Content-Security-Policy to switch it on.
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header always set Content-Security-Policy-Report-Only "default-src 'self' https:; img-src 'self' https: data:; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; upgrade-insecure-requests"
Header always set X-Frame-Options "SAMEORIGIN"
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=()"
</IfModule>Redirect everything to HTTPS
Turn on your host's free certificate first, then make sure both addresses in Settings > General start with https://.
How to fix it on WordPress (Apache .htaccess)
- In WordPress go to Settings > General and make sure both addresses start with https://.
- Add this to the top of your .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]Fix mixed content
Sites that moved to HTTPS often still have old http:// addresses stored in posts and settings. A search-and-replace across the database fixes them all at once. Back up first.
How to fix it on WordPress
- Install the 'Better Search Replace' plugin and replace http://example.com with https://example.com in all tables (back up first).
- For third-party files, change their links to https://. Most providers support it.
wp search-replace 'http://example.com' 'https://example.com' --all-tablesBlock private files
Backups, .env files and .git folders left in your WordPress folder can be downloaded by anyone. This rule blocks every hidden file except /.well-known/.
How to fix it on WordPress (Apache .htaccess)
- Treat every password and key in that file as stolen. Change them all now: database password, API keys, payment keys, email passwords.
- Delete the exposed file or folder from your web root.
- Add this to your .htaccess (Apache) so hidden files are always blocked.
- Afterwards, open the address again in a private window and confirm it shows an error page.
<FilesMatch "^\.(?!well-known)">
Require all denied
</FilesMatch>
RedirectMatch 404 /\.gitProtect login cookies
Cookies without the Secure, HttpOnly and SameSite flags are easier to steal. These PHP settings cover session cookies; cookies set by a plugin need that plugin's settings.
How to fix it on PHP
- Set these in php.ini (or your host's PHP options) to protect session cookies:
- For cookies set by a plugin, check that plugin's settings or ask its developer.
session.cookie_secure = 1
session.cookie_httponly = 1
session.cookie_samesite = LaxYour domain's email records
Add these wherever your domain's DNS lives, often your host's control panel or your registrar. WordPress sends email too (password resets, order emails), so include whichever service it sends through. Our email guide explains each record.
How to fix it
- Log in to wherever you manage your domain's DNS (your registrar, Cloudflare, etc.).
- Add both records below to example.com. If there's already a TXT record starting with v=spf1, replace it: a domain must have only one.
- Replace the <...> part with your email provider's SPF include: Google Workspace is _spf.google.com, Microsoft 365 is spf.protection.outlook.com. Add one for each service that sends email as you.
- If you're not sure every service that sends email as you is listed, start with p=none for two weeks and read the reports, then change it to p=quarantine.
Type: TXT
Name: @
Value: v=spf1 include:<your email provider's SPF domain> -all
Type: TXT
Name: _dmarc
Value: v=DMARC1; p=quarantine; rua=mailto:dmarc@example.comHow to check it worked
Each header should appear right after you save .htaccess. Or run our free checkup again.
curl -sI https://example.com | grep -iE 'strict-transport|x-frame|x-content-type|referrer-policy|permissions-policy|content-security'Common mistakes
- Pasting rules inside the
# BEGIN WordPressblock, where WordPress overwrites them. - Editing
.htaccesswithout a backup. One typo can take the site down; restoring the copy fixes it. - Leaving old plugin and theme folders in place after deactivating them.
What this can't tell you
We see what the public sees. Admin accounts, plugin settings and file permissions need a look from inside.
Related guides
- WordPress security: the five fixes that matter most
- How to add security headers to your website
- How to make your whole site use HTTPS
- Exposed .env or .git file? What to do now
Check your own site, free
The free checkup shows how many results are flagged and how serious. For $1, the report picks the ones that matter for your site and writes each fix for your platform, with your own domain filled in.