skip to Main Content

The site’s actual url is "allenbrookneighbors.org".

In settings/reading "your homepage display" is set to "a static page" and "Homepage:" is set to "Home". There is no "/home" directory in the site’s file structure. "Home.php" is just a blank static page; after wp’s heirarchy search, front-page.php is loaded when a user comes to the site.

There is a small amount of dynamic content—a quote about "neighbors"—on the front page which is intended to change when the page is refreshed.
It works beautifully in firefox desktop but not in chrome, edge, safari, or even firefox mobile.

We discovered that we could bypass these browsers’ local cache and get new dynamic data every time if the user appends "/home" to the site url: "allenbrookneighbors.org/home" and hits {enter}, but users don’t know to do this.

We thought we could write a redirect function that would simply add the "/home" modifier to the incoming url, but the page doesn’t load; we get a message saying, "The page isn’t redirecting properly. [Browser] has detected that the server is redirecting the request for this address in a way that will never complete."

add_action('template_redirect', 'redirect_home_url');
function redirect_home_url() {
  if (is_front_page() && !isset($_GET['redirected'])) {
    wp_redirect(home_url('/home'));
    exit;
  }
}

So the question is: Is there a way that works to automatically add "/home" to the url in the address bar (if it is not already there) so users will see the new content if they come to the page more frequently than their browser’s local cache expires? Or also, so if a user presses the refresh button after reading the content, they will see new content?

2

Answers


  1. It’s not clear to me the significance of Home.php. If you visit <yoursite>/home, it will not load that file (unless you have some funny htaccess/server config). Or did you mean Home.php static page in your WP template?

    You’re probably getting the infinite redirect because /home is still handled by WP and it doesn’t know that you want to stop redirecting when /home is reached. Additionally, the isset($_GET['redirected']) won’t work since that parameter is not being set (at least I can’t see that here: https://developer.wordpress.org/reference/functions/wp_redirect/#source).

    So I’m guessing that you’d need to at least use this instead:

    wp_redirect(home_url('/home?redirected=1'));
    

    How do I configure the server to "tell the browsers how long something should be cached…"

    This depends a bit on the server, but in principle you just need to set response headers, which means that you can also do it in PHP (if you are using PHP to server resources).

    Examples

    • Header set Cache-Control "max-age=86400, public"
      

      If you put that in an .htaccess, all affected resources should be cached by anyone for one day (86400s => 24h, public => anyone). See also https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control

    • <FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
        Header set Cache-Control "max-age=86400, public"
      </FilesMatch>
      

      Same as before, but only for those specific file extensions.

    • header('Cache-Control "max-age=86400, public"');
      

      The PHP version. Could be useful for, for example, sitemap generators, dynamic assets etc.

    Refreshing the cache

    The HTTP standards is very flexible, it provides ways to cache, invalidate as well as avoid caching.

    MDN provides some case scenarios as well as possibilities on busting the cache. See this for details: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#caching_static_assets_with_cache_busting

    It’s basically what I mentioned here: How do I add "/home" to the site's url

    Login or Signup to reply.
  2. I think you can actually configure a file that act as controller. This file receive all incoming requests, then check if file corresponding to the request exists(all in accordance with you configuration) and output the content

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search