How to Disable caching on specific links via htaccess?

Asked

Viewed 177 times

-2

After doing a lot of research on the subject, I will rephrase my question.

It’ll get clearer:

On my website Wordpress, i would like to set the expiration of html files only on the homepage of the website "homepage" so that the browser do not save the cache.

I tried using the code below to set the expiration of all html to 1 week, and of homepage "index.html" to 0.

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 1 month"

ExpiresByType text/html "access plus 1 week"
    <FilesMatch "^(index\.html)$">
        ExpiresActive On
        ExpiresByType text/html "access plus 0 seconds"
        Header append Cache-Control "public"
    </FilesMatch>
</IfModule>

But only the 1 week setup worked.

ExpiresByType text/html "access plus 1 week"

Configuration inside "Filesmatch" is completely ignored.

ExpiresByType text/html "access plus 0 seconds"

However, when I try to set file expiration Styles.css instead of index.html, using the same structure, the code works.

Thus, it seems that something related to the fact of defining the expiration specifically of the index.html file within "Filesmatch" prevents the code from working.

I know that wordpress does not own a file index.html, he owns a index php. which generates html. but probably the name of the html file used in the wordpress home page is not index. must be something else.

So the question is, how to set the expiration of the html file only on the home page?

1 answer

0

It seems that the purpose and operation of caches is not very clear to you. The goal of using caching is to avoid "heavy" and low probability processing. Cache mechanisms should be created for repetitive and unchanged processing during cache existence.

An example would be a homepage of a site with many visits. All visitors should see the same content so there is no need to make queries to the database for each visit, for example. In this case, you can "mount" the html of the page once and use it for all visitors.

A simple and common way to cache web pages is with specific headers that tell the user’s browser whether to take advantage of content they have already stored locally or whether to request to the server. The most common header for this is the cache-control (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control).

Depending on how the header is cache-control, in a subsequent request from the same user, the user’s own browser will verify that it already has the content locally and that it is within the defined expiration rules. Positive case, nay a request is even made to the server.

In addition to header cache-control, you can use the ETag. The purpose of this is for the browser to identify whether a URL has changed its content afterward expiration of the cache.

From what has been described, it does not seem to be what you need. Most likely you are looking for some cache on the server to prevent different users make requests that require resources from their server. To do this, you should look for other forms of cache, such as storing the final html generated by your site. There are numerous Wordpress plugins that do all this control automatically (and even will configure the headers properly for most file types like scripts, css and images).


Answering the questions more directly:

  1. You can do something like
<FilesMatch "<URL>">
    <IfModule mod_headers.c>
        FileETag None
        Header unset ETag
        Header unset Pragma
        Header unset Cache-Control
        Header unset Last-Modified
        Header set Pragma "no-cache"
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
        Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
    </IfModule>
</FilesMatch>
  1. There is no way to do this with Apache/configuration in . htaccess, so it makes no sense to ask. What in a way can be done is caso o cache tenha expirado check if there are changes in the content (for this can be used the ETag).

  2. Try reading first about different types of caching and where to use them. But if you’re looking for Apache . htaccess documentation, here it goes: https://httpd.apache.org/docs/2.4/howto/htaccess.html.

  • wordpress saves the search cache. if you do not reload the page after each search, you will continue to see outdated results. Need to disable cache only at this search URL. The code I was able to create generates a 500 error.

  • See my htaccess: # ALL - 1 YEAR CACHE <Ifmodule mod_expires. c> Expiresactive On Expiresdefault "access plus 1 year" </Ifmodule> # SEARCH RESULTS - 1 SECOND CACHE <Locationmatch "./?s=."> <ifModule mod_expires. c> Expiresactive On Expiresdefault "access plus 1 Second" </ifModule> </Locationmatch>

  • What cache are you talking about @lgdelai? And why cache 1s? If it’s a page with thousands of hits per second, it would make sense, but it still wouldn’t be in the polls.

  • And where it came from "The wordpress saved search cache. if you do not reload the page after each search, continue seeing outdated results."?

  • Hello @tvdias, thanks for your help so far, I researched a lot about it, and I reformulated my question, I think it will now be much clearer what I want. If you could take a look, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.