bypass Expires Settings

#1
Hello,

On Web browser, Genral page - Expires Settings. I instruct the webserver to cache HTML by add text/html=A900

On the website, I created a redirect. For example: mysite.com/random/ to a random page - 307 redirect type.

Due to setting on the server, the redirect is only work the first time, the cached by the browser for 900 second.

Is there a way to bypass the link mysite.com/random/ from the setting on server side?

Thank you.
 

Pong

Administrator
Staff member
#2
You may try to apply the following setting in .htaccess under /random/ directory.

<IfModule mod_expires.c>
ExpiresActive Off
</IfModule>
 
#3
Hello,

It''s a link for redirect so no directory.

I put code in function.php to create the function:

Code:
//random redirect
add_action('init','random_add_rewrite');
function random_add_rewrite() {
       global $wp;
       $wp->add_query_var('random');
       add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}

add_action('template_redirect','random_template');
function random_template() {
       if (get_query_var('random') == 1) {
               $posts = get_posts('post_type=post&orderby=rand&numberposts=1');
               foreach($posts as $post) {
                       $link = get_permalink($post);
               }
               wp_redirect($link,307);
               exit;
       }
}
So, is there a way to disable Expires on a link?

Thank you.
 
#6
Well, It does work but I ran to another issue.

I add on top of .htaccess:

Code:
<If "%{REQUEST_URI} = ~ /random/">
    ExpiresActive Off
</If>
The link /random/ no longer cached, but it also remove ExpiresActive of other type. I can no longer see max-age=900 in the header.

Is there something wrong with rule?

Thank you.
 

Pong

Administrator
Staff member
#7
Normally global setting are not recommended. LSWS Web Admin -> Genral page -> Expires Settings. text/html=A900 should be removed.

Instead, place it in <Else> </Else> section after your <If> condition in .htacess

<If "%{REQUEST_URI} = ~ /random/">
ExpiresActive Off
</If>
<Else>
ExpiresActive On
...
</Else>
 
#8
So, I tried it like this:

Code:
<If "%{REQUEST_URI} = /random/">
ExpiresActive Off
</If>
<Else>
ExpiresActive On
ExpiresByType text/html "access plus 15 minutes"
</Else>
Checking the header, I see the Else work, but the mysiteurl.com/random/ still be cached.

I also tried the full url but still the same.

If "%{REQUEST_URI} = 'mysiteurl.com/random/'

What is the correct way to use it please?

Thanks.
 
#10
we set up a centos 7 VM which apache 2.4 is installed. then installed latest lsws 5.1.11

my test result:
1. <If> <Else> is a new feature introduced in apache 2.4.
<If "%{REQUEST_URI} =~ /random/">
ExpiresActive Off
</If>
is working with apache 2.4, but not supported by lsws yet from my quick tests.

2. back to your original question:
On the website, I created a redirect. For example: mysite.com/random/ to a random page - 307 redirect type.
in .htaccess,
RewriteRule random/.* b.html [R=307]

then access it from apache 2.4 and lsws 5.1.11
Code:
#curl -I 127.0.0.1:1080/random/c.html
HTTP/1.1 307 Temporary Redirect
Date: Thu, 12 Jan 2017 09:44:46 GMT
Accept-Ranges: bytes
Server: LiteSpeed
Location: http://127.0.0.1:1080/b.html
Connection: Keep-Alive

#curl -I 127.0.0.1/random/c.html    
HTTP/1.1 307 Temporary Redirect
Date: Thu, 12 Jan 2017 09:45:16 GMT
Server: Apache/2.4.6 (CentOS)
Location: http://127.0.0.1/var/www/html/b.html
Content-Type: text/html; charset=iso-8859-1
you can see that for redirect, Exprires etc response headers are removed for both apache and litespeed.
in other word, "ExpiresActive On/Off" etc is completely ignored when http code is redirect (here 307)
 
#11
Unfortunately, on my test with on both Firefox and Chrome, the redirect is still be cache:

Request URL:https://mysite.com/random/
Request Method:GET
Status Code:307 (from disk cache)
Remote Address:xxxx:443
Response Headers
accept-ranges:bytes
cache-control:public, max-age=600
content-length:0
content-type:text/html; charset=UTF-8
date:Thu, 12 Jan 2017 16:00:45 GMT
expires:Thu, 12 Jan 2017 16:10:45 GMT
location:-
server:LiteSpeed
status:307
x-litespeed-cache-control:no-cache


I also tried:

RewriteRule /random/?$ [R=307]
Header always set Cache-Control "no-store, no-cache, must-revalidate" env=nocache


But still the same!
 
#13
if redirect comes from backend php script, I created a test case and just works.
Code:
#cat re.php
<?php

header("Location: /phpinfo.php");

?>
#cat .htaccess
RewriteEngine On

RewriteRule re.php - [E=reflg:1]

header unset Cache-Control env=reflg
header always set My-Cache-Control "no-store, no-cache, must-revalidate" env=reflg
without .htaccess
Code:
#curl -I 127.0.0.1/re.php
HTTP/1.1 302 Found
X-Powered-By: PHP/5.6.29
Location: /phpinfo.php
Content-Type: text/html; charset=UTF-8
Date: Sat, 14 Jan 2017 09:50:46 GMT
Accept-Ranges: bytes
Server: LiteSpeed
Cache-Control: no-cache, no-store, must-revalidate, max-age=0
Connection: Keep-Alive
with that .htaccess
Code:
#curl -I 127.0.0.1/re.php 
HTTP/1.1 302 Found
X-Powered-By: PHP/5.6.29
Location: /phpinfo.php
Content-Type: text/html; charset=UTF-8
Date: Sat, 14 Jan 2017 09:51:59 GMT
Accept-Ranges: bytes
Server: LiteSpeed
My-Cache-Control: no-store, no-cache, must-revalidate
Connection: Keep-Alive
 
Top