Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6741407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:43:15+00:00 2026-05-26T11:43:15+00:00

I want to conditionally set cache headers depending on what path files are accessed

  • 0

I want to conditionally set cache headers depending on what path files are accessed from. Basically, accessing http://www.example.com/cache/$cache_key/* should serve files with far in the future cache headers.

I’m using a rewrite rule to set an environment variable and then attempting to set cache control headers based on that variable. However, it seems like the variable is being set too late in the process or something; the conditional header rules are never getting executed.

RewriteRule ^cache/.*?/(.*) /$1 [env=asset:true,L]
Header set Cache-control "max-age=30"
Header set Cache-Control "max-age=31536000" env=asset
Header unset ETag env=asset

Is there a better way to do this? I’ve tried a couple of combinations of Directory and Location blocks with no success.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:43:16+00:00Added an answer on May 26, 2026 at 11:43 am

    Using phpinfo() I determined the environment variable ends up not being set at all on the rewritten request, so the problem isn’t the order of the request, it’s that it seems to toss the variable out. Using the query string instead of the URL and not rewriting seemed to be the only way I could get this working. I do agree, it seems like there should be a better way.

    RewriteCond %{QUERY_STRING} longcache=true(&|$)
    RewriteRule .* - [ENV=LONGCACHE:true,L]
    
    Header set Cache-Control "max-age=30" env=!LONGCACHE
    Header set Cache-Control "max-age=31536000" env=LONGCACHE
    

    MORE DIFFERENT ANSWER OBTAINED BY OPENING EYES:

    Your asset environment variable gets renamed to REDIRECT_asset after the redirect, so your conditional Header directive needs to be:

    Header set Cache-Control "max-age=31536000" env=REDIRECT_asset
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jQuery UI Dialog containing some checkboxes. I want to conditionally set
I want to conditionally output HTML to generate a page, so what's the easiest
I want to conditionally navigate to some page. If some condition is true i
In my code, I want to conditionally perform a few operations with: #ifdef DEBUG
I'm building a carArray and want to filter the contents conditionally, using an NSPredicate
I want to put a conditional breakpoint in windbg. For example lets say LoadLibrary
I am working on a form where I need to conditionally set the content
I'm using the great Facebook plugin for cakephp 1.3 by http://www.webtechnick.com . This is
I want my main layout to conditionally put in some blocks of html that
I only want to reboot when uninstalling. This is a fragment from my WiX

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.