Enabling compression in .htaccess file is causing 500 internal error

This is the code I am adding to my .htaccess file for compression and browser caching and gzip.

# BEGIN WordPress
IfModule mod_rewrite.c
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
/IfModule

# END WordPress

ifModule mod_gzip.c
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
/ifModule

## EXPIRES CACHING ##
IfModule mod_expires.c
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
/IfModule
## EXPIRES CACHING ##

When I go to the dashboard>settings>permalinks and click save changes, this code disappears and only the first block remains and the 500 internal error gets resolved. But my website is still not compressed or cached or gzipped. I need those three things to speed up the site.

Topic compression cache seo Wordpress

Category Web


When I go to the dashboard>settings>permalinks and click save changes, this code disappears

It sounds like WordPress is overwriting your htaccess file when you save. Thus, your changes are gone.

Perhaps set this up outside of the htaccess in the apache config? Or ask your host to do this for you. Then WP can't overwrite it :)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.