1

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.

Jai
  • 119
  • 7
  • What us your Apache version? – anubhava Sep 04 '22 at 06:06
  • "I have cat ids to mention in exception condition." - What do you mean by this? What "cat ids"? Are these in the URL (although not in your example)? – MrWhite Sep 04 '22 at 09:09
  • 1
    "I am ready to mention URL of each category (total categories around 30) in .htaccess." - Since there doesn't appear to be anything that differentiates the category and article URLs then this is what you would need to do to using `.htaccess`. However, I feel that this would be better resolved in WordPress itself. – MrWhite Sep 04 '22 at 09:52
  • Hi Anubhava, The Apache version is 2.4. Dear MrWhite cat id means Category id for each category. I mean to say there are category ids which if required can be mentioned in exception condition..There is option to add ExpiresActive OFF for specific urls which can be one alternate way out but don't have much information on that...Best rgds. – Jai Sep 04 '22 at 09:52
  • 1
    Can you provide few more examples of category and article page. (Don't use `.....`) – anubhava Sep 04 '22 at 10:00
  • Few category pages are: abc.com/garmin.html abc.com/huawei.html abc.com/itel.html Few articles URLs are abc.com/boat-wave-connect-features-list-alexa.html abc.com/nokia-g200-5g-features-list.html abc.com/samsung-galaxy-m65-features-list-2022.html – Jai Sep 04 '22 at 10:46
  • http://www.price4india.co.in/ is the site in question, plz check here for more details. best rgds – Jai Sep 04 '22 at 10:49
  • 1
    @Jai From those few examples it would seem category URLs do not contain hyphens, whereas article URLs do. Is that consistent for all categories? (Although I imagine that could be difficult to enforce in the future?) – MrWhite Sep 04 '22 at 10:58
  • Dear MrWhite When there are more than one word in category url, they too have hyphens e.g. abc/mobile-gadgets.html . rgds – Jai Sep 04 '22 at 11:28

1 Answers1

1

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>
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Both category and articles pages end with html but I need 1 day expiration for category pages and 7 day for article pages. I think the above mentioned code will set 1 day expiration for both category and article pages. rgds – Jai Sep 04 '22 at 10:47
  • Can anyone suggest if solution mentioned at https://wp-mix.com/disable-caching-html/ will work. It says: – Jai Sep 04 '22 at 13:04
  • Can you include those `` tags just for category pages inside your WP theme? – anubhava Sep 04 '22 at 14:58
  • 1
    @Jai Maybe not and not without additional work. Any `Cache-Control` HTTP response headers (such as those you are currently setting with mod_expires) will override the ` – MrWhite Sep 05 '22 at 01:43
  • 1
    @MrWhite Agree with you completely. Also I am using Super cache plugin, so meta in header, ExpiresDefault in htaccess and Super Cache all three together in a combination will work is very rare chance and for sure significant work. If not now at least in future the combination will turn out problematic. any ways thnx for urs and Anubhava support. best rgds – Jai Sep 05 '22 at 05:27