DEV Community

nabbisen
nabbisen

Posted on

WordPress: My Additional Parts To .htaccess In Production

Introduction

I have 3 sections in .htaccess of WordPress websites to be added when I publish them.
One is for security and the others are for performance.

My Additional Parts

Section #1: Denial Of .git Access

It's for security.
I never want .git logs exposed even by accident although I don't have a habit to use Git in production environment.

RedirectMatch 404 /\.git
Enter fullscreen mode Exit fullscreen mode

Section #2: Compression Of Data Transfer

It's for performance.
(I like checking my websites with Google's web.dev / PageSpeed Insights.)

<IfModule mod_deflate.c>
# compress html/css/javascript/text/xml/font
AddOutputFilterByType DEFLATE application/javascript
...
</IfModule>
Enter fullscreen mode Exit fullscreen mode

Section #3: Browser Caching

It's also for performance.

<IfModule mod_deflate.c>
ExpiresActive On
ExpiresByType image/gif "access plus 3 weeks"
...
</IfModule>
Enter fullscreen mode Exit fullscreen mode

Conclusion

To sum up, my .htaccess in production is like this:

# BEGIN WordPress
...

# END WordPress

RedirectMatch 404 /\.git

<IfModule mod_deflate.c>
# compress html/css/javascript/text/xml/font
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml

# escape from bugs of pretty old browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>

<IfModule mod_deflate.c>
ExpiresActive On
ExpiresByType image/gif "access plus 3 weeks"
ExpiresByType image/jpeg "access plus 3 weeks"
ExpiresByType image/png "access plus 3 weeks"
ExpiresByType image/x-icon "access plus 2 weeks"
ExpiresByType text/css "access plus 1 weeks"
ExpiresByType text/javascript "access plus 1 weeks"
ExpiresByType application/javascript "access plus 1 weeks"
ExpiresByType application/x-javascript "access plus 1 weeks"
</IfModule>
Enter fullscreen mode Exit fullscreen mode

I'm happy if you would tell me about how to correct or advance them 😄

Happy serving 🕊

Top comments (0)