Every time a visitor lands on your website, their browser has to download all your logos, CSS stylesheets, and JavaScript files. This takes time and consumes server bandwidth. By leveraging browser caching, you can instruct the visitor's browser to save these static files locally, so subsequent pages load almost instantly.

While some WordPress plugins can handle this, the most efficient and direct way to enable browser caching is by adding a few lines of code to your website's hidden configuration file right inside your cPanel.

Step 1: Locating the .htaccess File in cPanel

Your .htaccess file is a powerful server configuration file located in your website's main directory.

  • Log in to your Sternhost cPanel account.

  • Navigate to the Files section and click on File Manager.

  • In the top right corner, click on Settings and ensure the box for Show Hidden Files (dotfiles) is checked.

  • Open your website's root folder (usually public_html).

  • Locate the .htaccess file, right-click it, and select Edit.

Step 2: Adding the Expiration Rules

Once you are in the editor, you will add "ExpiresByType" rules. This code tells the browser exactly how long it should hold onto specific file types before asking the server for a fresh copy.

  • Scroll to the very bottom of the file. Do not paste this inside existing WordPress tags to avoid it getting overwritten.

  • Copy and paste the following snippet:

Apache
 
<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 application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
  • Click the Save Changes button in the top right corner of the editor.

     

Why This Method Works Best

 
  • Zero Plugin Overhead: You avoid adding yet another plugin to your WordPress dashboard, which keeps your site lightweight.

     
  • Immediate Impact: This change is instantly recognized by tools like Google PageSpeed Insights, effectively clearing the common browser caching warnings.

  • Server Resource Savings: Because repeat visitors load static files from their own hard drive instead of your server, your overall hosting bandwidth usage will drop significantly.

Ha estat útil la resposta? 21 Els usuaris han Trobat Això Útil (1 Vots)