DEV Community

Cover image for WordPress Optimization Techniques Using .htaccess
Muhammad Usman
Muhammad Usman

Posted on

4 2 2 2 2

WordPress Optimization Techniques Using .htaccess

Optimizing WordPress using the .htaccess file involves implementing directives to enhance site performance, security, and functionality. Below are key techniques with examples:

1. Enable GZIP Compression

Compress files to reduce their size and speed up loading times.

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json application/x-javascript application/xml application/xhtml+xml application/rss+xml application/atom_xml application/font-woff application/font-woff2 image/svg+xml
</IfModule>
Enter fullscreen mode Exit fullscreen mode

2. Leverage Browser Caching

Cache static resources in the user's browser.

<IfModule mod_expires.c>
ExpiresActive On ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
Enter fullscreen mode Exit fullscreen mode

3. Prevent Hotlinking

Stop other sites from embedding your images.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_REFERER} !^$
  RewriteCond %{HTTP_REFERER} !^https://(www\.)?yourwebsite\.com [NC]
  RewriteRule \.(jpg|jpeg|png|gif)$ - [F,NC,L]
</IfModule>
Enter fullscreen mode Exit fullscreen mode

4. Block Bad Bots

Prevent access from known malicious bots.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_USER_AGENT} ^.*(badbot|evilbot|maliciousbot).*$ [NC]
  RewriteRule .* - [F,L]
</IfModule>
Enter fullscreen mode Exit fullscreen mode

5. Disable Directory Browsing

Prevent listing files in directories.

Options -Indexes
Enter fullscreen mode Exit fullscreen mode

6. Limit Access to wp-config.php

Secure your critical WordPress configuration file.

<Files wp-config.php>
order allow,deny
deny from all
</Files>
Enter fullscreen mode Exit fullscreen mode

7. Protect .htaccess File

Prevent others from modifying the .htaccess file.

<Files .htaccess>
order allow,deny
deny from all
</Files>
Enter fullscreen mode Exit fullscreen mode

8. Redirect HTTP to HTTPS

Force your website to use HTTPS.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
Enter fullscreen mode Exit fullscreen mode

9. Limit Post Request Size

Prevent large requests to mitigate DoS attacks.

LimitRequestBody 10485760
Enter fullscreen mode Exit fullscreen mode

10. Enable CORS

Allow resources to be shared across domains (useful for APIs and fonts).

<IfModule mod_headers.c>
  Header set Access-Control-Allow-Origin "*"
</IfModule>
Enter fullscreen mode Exit fullscreen mode

11. Restrict Access to Admin Area

Restrict wp-admin access to specific IPs.

<FilesMatch "wp-login.php">
  order deny,allow
  Deny from all
  Allow from 123.456.789.0
</FilesMatch>
Enter fullscreen mode Exit fullscreen mode

12. Enable Keep-Alive

Improve connection handling.

<IfModule mod_headers.c>
  Header set Connection keep-alive
</IfModule>
Enter fullscreen mode Exit fullscreen mode

13. Remove ETags

Reduce overhead by disabling ETags.

<IfModule mod_headers.c>
  Header unset ETag
</IfModule>
FileETag None
Enter fullscreen mode Exit fullscreen mode

14. Optimize Default WordPress .htaccess

Include clean permalink rules and other optimizations.

# BEGIN WordPress
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Enter fullscreen mode Exit fullscreen mode

These optimizations should be tested thoroughly to ensure compatibility with your WordPress setup and server configuration. Back up your .htaccess file before making changes.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay