A complete, step-by-step guide to hardening your WordPress website at the server level. Learn how to secure core files, restrict dangerous script execution, enforce browser HTTP security headers on Apache web servers, and implement essential application safeguards.
Important: Before modifying any .htaccess file, make a backup of the original. Incorrect configuration can cause website errors or prevent access to your website. Always test the changes after applying them.
1. Uploads Hardening: PHP Script Execution Prevention
To block direct access to PHP files in the WordPress uploads folder, add this code to wp-content/uploads/.htaccess:
<Files *.php>
<IfModule mod_authz_core.c>
# Apache 2.4+
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
# Apache 2.2 (Legacy)
Order Allow,Deny
Deny from all
</IfModule>
</Files>
> To verify the restriction: Create a temporary file named security-test.php inside wp-content/uploads/ and visit https://yourdomain.com/wp-content/uploads/security-test.php. The server should return 403 Forbidden, indicating that access to the PHP file is blocked. Delete the test file afterwards.
2. XML-RPC Hardening: Remote Request & Pingback Protection
XML-RPC is a WordPress protocol used for remote publishing and pingbacks. Disabling it when not needed reduces exposure to automated login attacks and pingback abuse. To disable XML-RPC add this code in root .htaccess:
<Files xmlrpc.php>
<IfModule mod_authz_core.c>
# Apache 2.4+
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
# Apache 2.2 (Legacy)
Order Allow,Deny
Deny from all
</IfModule>
</Files>
> Alternatively, add this code to your theme’s functions.php to disable XML-RPC methods requiring authentication. This does not completely disable XML-RPC. The .htaccess method above is preferable because it blocks access at the server level before WordPress loads:
add_filter( 'xmlrpc_enabled', '__return_false' );
> To verify the .htaccess restriction, visit https://yourdomain.com/xmlrpc.php – it should return HTTP 403 Forbidden.
3. Configuration Security: Core System File Access Restrictions
To protect your wp-config.php file, which contains your database credentials and secret keys, add this code to the root .htaccess:
<Files wp-config.php>
<IfModule mod_authz_core.c>
# Apache 2.4+
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
# Apache 2.2 (Legacy)
Order Allow,Deny
Deny from all
</IfModule>
</Files>
> To verify the restriction, visit https://yourdomain.com/wp-config.php and check that the server returns HTTP 403 Forbidden. A blank page alone does not confirm that access is blocked. You can check the HTTP status using your browser’s Developer Tools (Network tab).
4. Directory Security: Directory Index & Browsing Protection
To block viewing the raw list of files and folders when there is no default index page add this code in root .htaccess file:
Options -Indexes
> To verify the restriction: Create a temporary empty folder named `security-test` in your website’s root directory and visit `https://yourdomain.com/security-test/`. The server should return **403 Forbidden**, indicating that directory browsing is disabled. Delete the test folder afterwards.
5. Browser Security: HTTP Security Headers Hardening
Enforce browser security policies to prevent MIME-sniffing and clickjacking, control referrer information, enforce HTTPS for returning visitors, and restrict access to browser features such as geolocation, microphone and camera. Add this code in root .htaccess:
<IfModule mod_headers.c>
# 1. X-Content-Type-Options: Prevents MIME-sniffing
Header always set X-Content-Type-Options "nosniff"
# 2. X-Frame-Options: Prevents clickjacking
Header always set X-Frame-Options "SAMEORIGIN"
# 3. Referrer-Policy: Controls navigation data leakage
Header always set Referrer-Policy "strict-origin-when-cross-origin"
# 4. Strict-Transport-Security (HSTS): Enforces HTTPS for one year
Header always set Strict-Transport-Security "max-age=31536000"
# 5. Permissions-Policy: Blocks misuse of device sensors like camera/mic
Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
# 6. Content-Security-Policy (CSP): Restricts potentially unsafe browser behaviour
Header always set Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'self'"
</IfModule>
Important: Only enable HSTS if your entire website works correctly over HTTPS. Once a browser receives this header through HTTPS, it will automatically enforce HTTPS for future visits for one year. Browsers ignore HSTS headers received over HTTP.
> Check headers in Security Headers: https://securityheaders.com/
> If these headers do not work in .htaccess they could be added using PHP in functions.php
Combined code for root .htaccess
The following code combines sections 2, 3, 4 and 5 into a single configuration for the website’s root .htaccess file. It supports Apache 2.4+ and Apache 2.2 (Legacy), with one shared rule protecting both xmlrpc.php and wp-config.php.
# XML-RPC & Configuration Security
<FilesMatch "^(xmlrpc|wp-config)\.php$">
<IfModule mod_authz_core.c>
# Apache 2.4+
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
# Apache 2.2 (Legacy)
Order Allow,Deny
Deny from all
</IfModule>
</FilesMatch>
# Directory Security
Options -Indexes
# Browser Security
<IfModule mod_headers.c>
# 1. X-Content-Type-Options: Prevents MIME-sniffing
Header always set X-Content-Type-Options "nosniff"
# 2. X-Frame-Options: Prevents clickjacking
Header always set X-Frame-Options "SAMEORIGIN"
# 3. Referrer-Policy: Controls navigation data leakage
Header always set Referrer-Policy "strict-origin-when-cross-origin"
# 4. Strict-Transport-Security (HSTS): Enforces HTTPS for one year
Header always set Strict-Transport-Security "max-age=31536000"
# 5. Permissions-Policy: Blocks misuse of device sensors like camera/mic
Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
# 6. Content-Security-Policy (CSP): Restricts potentially unsafe browser behaviour
Header always set Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'self'"
</IfModule>
Important: Use this combined configuration instead of the individual code examples in sections 2–5, not in addition to them. Add it to your existing root .htaccess file without removing WordPress rewrite rules or other necessary directives.
The code for section 1 has to be added in the wp-content/uploads/.htaccess file only.
Application Security: Core Safeguards & Plugin Defenses
Securing your WordPress files at the server level is only half the battle. Because WordPress runs on dynamic PHP and relies heavily on plugins, you must also defend the application layer from unauthorized access, brute-force attacks, and vulnerable code execution.
1. Malware & File Integrity Scanning
An automated process that checks WordPress files against known originals where available and scans for malware signatures, backdoors and suspicious code changes.
2. Brute-Force & IP Locking
A security control that tracks failed login attempts and automatically bans aggressive IP addresses or bots from accessing your site.
3. Two-Factor Authentication (2FA)
A login security layer that requires both a standard password and a temporary passcode generated by an authenticator app (e.g., Google Authenticator, Authy) on a mobile device.
4. Login CAPTCHA Protection
An automated verification check (such as Google reCAPTCHA) added specifically to your login screen to block automated bots and brute-force scripts from submitting fake credentials.
These four security measures can be implemented using the free Wordfence plugin: https://www.wordfence.com/
> Related article, Is my Website Hacked? : https://www.danielmrey.com/is-my-website-hacked