How to disable cache on specific links via htaccess?

Discussion in 'Apache' started by lgdelai, Aug 28, 2020.

  1. #1
    01 - I would like to disable the browser cache for a specific link.

    For example:
    In wordpress, if you search for a term, "test" the search result will display the url:
    htt ps://site . com/?s=test

    I want to disable the cache for all links that start with:
    htt ps://site . com/?s=

    In this way, search results would never be cached.


    02 - I would like to know if it is possible to have more than one cache rule, where each rule was used depending on a condition.

    For example, on a wordpress site, when accessing the site, the htacces rule would check if you hear any changes to the site since the last visit.

    * If there was no change, the browser cache will be used.

    * If there was a change, the cache would be disabled, so the browser will search to download the updated version of the files.


    03 - Where can I find a manual or something, explaining how to use htaccess programming, I couldn't find it on the web. I always find ready-made codes, but I can't find explanations of the meaning of each part.

    Thankful.
     
    lgdelai, Aug 28, 2020 IP
  2. Daniel165

    Daniel165 Member

    Messages:
    145
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    You can try this below script:

    <Directory /some/real/path/to/tmp>
    Header Set Pragma "no-cache"
    Header Set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
    Header Set Cache-Control "max-age=0, no-store, no-cache, must-revalidate"
    Header Unset ETag
    FileETag None
    </Directory>
     
    Daniel165, Nov 10, 2020 IP
  3. lgdelai

    lgdelai Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Hi. Daniel, thanks for your help.

    I tried to add your code but I got error 500 in every attempt I made, see what I tried:

    I try this on end of my htaccess:
    <Directory /?s=>
    Header Set Pragma "no-cache"
    Header Set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
    Header Set Cache-Control "max-age=0, no-store, no-cache, must-revalidate"
    Header Unset ETag
    FileETag None
    </Directory>

    I try this too
    <IfModule mod_expires.c>
    # Enable expirations
    ExpiresActive On
    # Default directive
    ExpiresDefault "access plus 1 month"
    # My favicon
    ExpiresByType image/x-icon "access plus 1 year"
    # Images
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"
    # Video
    ExpiresByType video/mp4 "access plus 1 year"
    ExpiresByType video/mpeg "access plus 1 year"
    # CSS, JavaScript
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    # Others
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"

    <Directory /?s=>
    Header Set Pragma "no-cache"
    Header Set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
    Header Set Cache-Control "max-age=0, no-store, no-cache, must-revalidate"
    Header Unset ETag
    FileETag None
    </Directory>

    </IfModule>



    Remembering that I want all search results not to be cached.
    That is, all URLs that begin with "MyDomain /?s=" they must not be cached...

    MyDomain /?s=search1
    MyDomain /?s=search2
    MyDomain /?s=search3
    MyDomain /?s=search ...



    As explained, this is necessary for searches to always return updated results.
     
    lgdelai, Dec 1, 2020 IP