Disable caching on specific links

Alles, was den Apache betrifft, kann hier besprochen werden.

Disable caching on specific links

Postby lgdelai » 29. August 2020 03:40

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:
https://site.com/?s=test

I want to disable the cache for all links that start with:
https://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
 
Posts: 2
Joined: 29. August 2020 03:28
XAMPP version: 0001
Operating System: 0001

Re: Disable caching on specific links

Postby Nobbie » 30. August 2020 12:34

You can configure Apache to apply HTTP Headers to your documents, which tells the browser how to cache a certain response. But that is not the same thing as you want. See "cache-control" documentation.

The main problem is, Apache does not know if your browser already caches a site, Apache does not know what is the last "version" of a document what is known to the browser. Apache only can tell the browser whether to cache a response, how long to keep the document in the cache etc. Apache cannot check the cache status of the browser. With the help of session and cookie variables you can try to build a kind of "memory", but you have to do this on yourself and there is no automatic memory build in Apache.

.htaccess is part of httpd.conf basic configuration. It is used for for all kind of options, which usually can be used in a <Directory>...</Directory> block. Instead of putting the code into that text block, you can put it into a seperate file called ".htaccess" and store it exactly to that Directory, which is aimed instead by the <Directory> option. So the syntax is identically to syntax of httpd.conf and you can use all configuration options, which are valid for <Directory> option. The idea of using .htaccess is, that you can put configuration into a user file, you do not need to be a system administrator and you dont need to restart Apache (as you would have to do if you change httpd.conf instead). Here is one of many tutorials about .htaccess: https://www.askapache.com/htaccess/
Nobbie
 
Posts: 13183
Joined: 09. March 2008 13:04

Re: Disable caching on specific links

Postby lgdelai » 30. August 2020 21:19

First of all, thanks for the feedback.

I studied the material on the link you sent me and I tried to create the code to do what I need but it doesn't work.

Wordpress search results always contain the following url:
example.com/?s=sometext

So I created the code below to remove the cache in all urls that have this part in their code.
/?s=

See my htaccess code

Code: Select all
# 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>


But I get an internal error 500 when I add to htacces.

Can someone help me?
lgdelai
 
Posts: 2
Joined: 29. August 2020 03:28
XAMPP version: 0001
Operating System: 0001

Re: Disable caching on specific links

Postby Nobbie » 31. August 2020 11:20

Error 500 usually is a severe error, unknown options (often due to a typo), or option not supported by the Apache release, or or or. See the logfile (i.e. c:/xampp/apache/logs/error.log) for a more detailed error message.

I see two more problems:

1) From the mod_expires documentation:

"When the Expires header is already part of the response generated by the server, for example when generated by a CGI script or proxied from an origin server, this module does not change or add an Expires or Cache-Control header.". WordPress is a highly sophisticated CGI script, its quite imageable, that WordPress already modifies the Expires Header for caching. If so, youre code will be ignored!

2) Your LocationMatch matches against the URL parameters "s=....". I dont think, these parameters are part of the Location URL. A location usually is only the part of the URL, which contains the logical path to the request, the parameters usually are handled separately. You should run some basic tests about that.

P.S.: I looked into http://httpd.apache.org/docs/current/mo ... ationmatch and it clearly says:

"The specified location matches exactly the path component of the URL."

Exactly what i said. That means, it does not include the parameters. I have no clue how to access these in a LocationMatch clause. Maybe you have to write your own CGI script instead.
Nobbie
 
Posts: 13183
Joined: 09. March 2008 13:04


Return to Apache

Who is online

Users browsing this forum: No registered users and 20 guests

cron