More .htaccess tips
Published
Updated
More .htaccess tricks.
Short `.htaccess` recipes that work on fortrabbit: lock down WordPress entry points, keep archives and dotfiles private, and show custom error pages.
Comment the rules
Lines starting with # are comments. Note what a rule does and why, next to the rule. Regular expressions are hard to read a year later.
Lock down WordPress
Block XML-RPC, which WordPress sites rarely need and bots attack often. <Location> does not work in .htaccess; use <Files> or <FilesMatch>:
<Files xmlrpc.php>
Order Allow,Deny
Deny from all
</Files>
Allow the login page from one IP only:
<Files wp-login.php>
Order Deny,Allow
Deny from all
Allow from 203.0.113.42
</Files>
The incoming firewall (WAF) also blocks xmlrpc.php when it is on. More on IP rules in block IPs.
Block file types
Keep backups and archives in the root path from being downloaded:
<FilesMatch "\.(bak|sql|zip|tar|tgz)$">
Order Allow,Deny
Deny from all
</FilesMatch>
The better fix is to keep such files outside the root path. Apache on fortrabbit never serves .ht* and .user.ini files, but a .env file or a .git folder inside the root path is reachable. Block all paths that start with a dot, except .well-known:
RewriteEngine On
RewriteRule (^|/)\.(?!well-known/) - [F]
Custom error pages
fortrabbit shows its own error pages for 4xx and 5xx responses that Apache creates. Replace one with ErrorDocument:
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
The path is relative to the root path and must be reachable over HTTP. Errors that the app handles itself, such as a 404 from the router of a framework, use the app's error page instead.