Customise htaccess Expires header setting for cache control for home, category URLs

727 views Asked by At

I am using following code in .htaccess to set Expires header status in WordPress site and its working fine.

## EXPIRES HEADER CACHING ##

<IfModule mod_expires.c>

ExpiresActive On

ExpiresByType image/jpg "access 1 year"

ExpiresByType image/jpeg "access 1 year"

ExpiresByType image/x-icon "access 1 year"

ExpiresDefault "access 7 days"
</IfModule>

Now I need to customise default Expires status for Home page and Category pages as they shall expire much faster i.e. 1 day.

The URL format is:

  • home page: example.com

  • category page: example.com/nokia.html

  • article page: example.com/.......html

Both category and article pages have .html file extension. If required there are category ids which can be mentioned in exception condition in htaccess in the solution (they are not part of category URLs).

In worst case scenario I am ready to mention URL of each category (total categories around 30) in .htaccess.

Summary: Default header expire in .htaccess set for 7 days for all URLs but for home page and category pages set it for 1 day.

Please share your tip to make it happen.

1

There are 1 answers

5
anubhava On

Using If expression in Apache 2.4 you can do:

# set 1 day expiration for landing page or for any page ending with .html
<If "%{REQUEST_URI} =~ m#(^/|\.html)$#">
   ExpiresDefault "access 1 days"
</If>