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.

9
  • What us your Apache version? Commented Sep 4, 2022 at 6: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)? Commented Sep 4, 2022 at 9: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. Commented Sep 4, 2022 at 9:52
  • 1
    Can you provide few more examples of category and article page. (Don't use .....) Commented Sep 4, 2022 at 10:00
  • 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?) Commented Sep 4, 2022 at 10:58

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

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
Can anyone suggest if solution mentioned at wp-mix.com/disable-caching-html will work. It says: <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"> <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Expires" content="0">
Can you include those <meta ..> tags just for category pages inside your WP theme?
@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 <meta "http-equiv" elements in the HTML source. (So you would need to remove the ExpiresDefault entirely or set this conditionally for other pages, which is probably more work than targeting "categories" only.) You also can't test that it is working by checking the "header/server response" as that article suggests, except to check that the HTTP headers are omitted entirely.
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.