Unable to utilize browser caching on WordPress with .htaccess file

Here is the code snippet from my WordPress .htaccess file that I am using:


## EXPIRES CACHING ##
<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 application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "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>
## EXPIRES CACHING ##

Despite implementing this code, I am still encountering numerous caching issues in Google Pagespeed.

Answer №1

Everything looks good with your syntax. If you're having trouble, it might be because mod_expires isn't enabled.

  • To enable mod_expires, use the command: a2enmod expires
  • After that, restart Apache by running: /etc/init.d/apache2 restart

Answer №2

Give this a shot:

# Implement browser caching for various static assets, set to 1 month
<filesMatch ".(css|jpg|jpeg|png|gif|pdf|js|ico|svg|ttf|woff|woff2)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>

Adjust the max-age according to your preference in seconds. I've chosen 1 month as my setting.

This approach uses cache-control instead of expires. Cache-control is considered more up-to-date. For a comprehensive overview of the variances and how to effectively utilize each method, refer to:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Encountering a 500 internal server error while working with

Currently, I have set up an ajax functionality with two different contents retrieved from separate files. Files: my_site_id_one.php: <div class="id_one_content"> <?php echo do_shortcode('[shortcode_one]'); ?> </div> my_ ...

I am looking to develop a straightforward login system. When a user decides to register, the following information will be displayed

Sorry, the requested object is not found! The URL you are looking for does not exist on this server. It's possible that the link on the page you came from is incorrect or outdated. Please notify the author of the page about this error. If you believe ...

Why isn't the JQUERY AJAX request showing up on the PHP page?

I have a total of 3 files - CallTasks.JS, opentask.php, and calltask.php. My goal is to make an AJAX call in CallTasks.JS to reach calltask.php, passing a string value to it and displaying the result on opentask.php. I am utilizing a JQUERY selector to ins ...

Fetch data from XML-RPC using PHP

I need to extract data from a table and display it in a list using a PHP script. $url_obj='http://'.$host.':8069/xmlrpc/object'; $sock=new xmlrpc_client($url_obj); $msg=new xmlrpcmsg('execute'); $arrVal=array('user_id&ap ...

Transforming the content of a <div> into a variable in PHP

There is a website where you can place orders for various products. The titles of the products available to order are embedded in the HTML code like this: <div class="whatever"> Title </div> I am trying to extract this "title" and assi ...

Encountered a syntax hiccup in my PHP and JavaScript codes

Below is my PHP code snippet: echo ("<td><img src='edit.jpg' width='20' alt='Edit' title='EDIT DATA' onClick=\"swipe2('" + . mysql_result($result, $i, 'no'). + '');'style= ...

PHP: Parameterizing Methods with Inheritance

Imagine that I have a PHP Class: class MyClass { public function doSomething() { // perform an action } } Now, let's say I derive from this class and override the doSomething method: class MyOtherClass extends MyClass { public functio ...

Having difficulty utilizing WP_Query effectively for accomplishing certain tasks

Currently, I am diving into the world of WordPress theme development by adapting my existing Bootstrap and JavaScript website. My requirements are as follows: 1) Implement a section on the homepage of my WordPress site to display the latest three blog ar ...

Refreshing product names in URLs

I am looking to rewrite a rule for my products. My goal is to utilize the id and name in the URL, separated by a dash like this: 123-name translates to product id = 123 and name = name This way, in my PHP code, I can fetch the $_GET[id] and then query ...

Investigating the presence of a file within the superior directory with RewriteCond

I am attempting to enhance this .htaccess file in order to redirect all requests to a script when the requested file or directory is non-existent. However, I want to refactor it so that it checks for the existence of the file in the parent folder. Curren ...

PHP Mailer ensures the integrity of messages

Recently, I discovered the power of PHP mailer and it truly amazed me. With the ability to attach files and perform various functions, it opens up a world of possibilities. However, a concern that arises is how to ensure the security of our email address ...

Mastering the Art: Efficiently Breaking Down Laravel 5.2's Query Builder!

I have three variables that I need to check before adding a specific where clause. If these variables are not null or blank, the where clauses should be added. $brand = $request->brand == 0 ? "" : $request->brand ; $category = $request->category ...

PHP and MySQL: Retrieve data based on user-specified time zones

Within my database, there is a table that tracks hits at different times. An example of this table looks like: id | datetime | ... ----------------------------------- 1 | 2014-05-30 15:19:00 | ... 2 | 2014-06-01 12:14:00 | ... 3 | ...

What steps should I take to include additional classes in this code?

Below is the code snippet currently implemented on my website: <?php foreach ($items as $key=>$item): ?> <li class="<?php echo ($key%2) ? "odd" : "even" ; if(count($items)==$key+1) echo ' lastItem'; ?>"> The current output ...

I'm feeling overwhelmed because I need to familiarize myself with either Typo3 or Drupal, and I'm not sure how to

Currently working on an exciting project that requires fast learning of Typo3 or Drupal. While I already have some basic knowledge in place, I successfully completed the installations for both platforms and can set up a basic site using Fluid Template in T ...

Troubleshooting: jQuery $.post not functioning as expected in certain circumstances

I'm currently experimenting with inserting data into a MySQL database using AJAX and jQuery's $.post method. To test this functionality, I have created the following setup: In my index.html file (which already includes jQuery for various other f ...

I can't seem to figure out why this file upload error is happening. What could be the

Having trouble with a simple file upload function and keep getting an "Error" message when trying to use it. Here's my HTML file: <form enctype='multipart/form-data' action='upload.php'> <input type='file' name ...

Constantly encountering incorrect passwords while attempting to authenticate using password_verify with database connections

When I try to log in using password_verify with my database, it keeps showing an error message stating that the password is incorrect. In my database, I have set the password field as char 255. function login(){ global $db, $username, $errors; ...

How to prevent form responses from being saved in the database using Laravel

I've been working on a registration page, but the data is not getting sent to my database. Strangely, no errors are being reported. Below is the code from the controller (Generated by Laravel): namespace App\Http\Controllers\Auth; us ...

Utilizing ajax and Laravel controllers to send multiple posts to an endpoint with varying IDs and Tokens through looping

Currently, I am working with Laravel to handle form data, an ajax call, and a controller that will send a message to multiple endpoints. However, I am facing some confusion regarding the looping structure. The endpoint requires the page ID, token, and mes ...