Can .htaccess test for If-Modified-Since?

Posted on 2009-02-23
Medium Priority
Last Modified: 2012-05-06
Can I use .htaccess to test if a browser sends this to in the header to the server:  
      If-Modified-Since: XXXXXXXXX GMT

and if so redirect the visitor to:
Question by:hankknight
  • 2
LVL 27

Expert Comment

ID: 23714888
Yes, of course
RewriteEngine on
# check
RewriteCond %{HTTP:If-Modified-Since} ^(.+)
# and pass as query strng q=
RewriteRule !^304\.php$ /304.php?q=%1 [L]

Open in new window

LVL 16

Author Comment

ID: 23715064
caterham_www, I think you clearly understand what I am trying to do.

If a server has already sent a file or an image I do NOT want to send it again.  Instead I want to tell them to stop wasting time and bandwidth by sending them a 304 header.

Will this work the way I intend it to?  Will it save website visitors time and my server bandwidth?

After giving my last question some thought, do you think it will work about as good as your ideas to modify and  recompiling the mod_rewrite.c module or make a custom Apache module?
   // 304.php
   header('HTTP/1.1 304 Not Modified');

Open in new window

LVL 27

Accepted Solution

caterham_www earned 2000 total points
ID: 23715334
Using a module is of course the fastest way, followed by the RewriteMap external program approach (which is loaded once at server startup) and followed by a processing of a script within a content handler.

Serving static files is pretty fast of course with sendfile (EnableSendfile). Until some point the downloading procedure of a small static file might be faster than processing a file in a content handler to issue a  304 by script.

> Will this work the way I intend it to?

Yes, you can do that within a php script as well, but there must not be any output (e.g. a blank line above of <? or below the closing ?>), of course. I'll post a small perl approach to the other thread.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question