0

I'd like to use an htaccess file to redirect all media requests to a PHP file. The PHP file will analyse the filenames to see if they are in a list and if not, it will load the media files regularly.

I'd like to ensure everything about this works normally. As in, caching won't break. Do I need to do anything special with the PHP file?

Brandon
  • 16,382
  • 12
  • 55
  • 88

3 Answers3

1

Sending headers like this example would work.

Isn't this a problem BTW that can be partially handled by a RewriteCond %{REQUEST_FILENAME} !-f & just letting Apache handle existing files? Just asking...

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • 1
    Yes, that simplifies things a lot: "only rewrite to a script if file doesn't exist" - you'll want to serve the same caching headers as Apache does, though (particularly the `ETag` and `Last_modified`). – Piskvor left the building Jun 06 '11 at 16:52
  • Actually that isn't what I am trying to do. I need to see if an existing file is on a list of files not to serve to nonmembers. – Brandon Jun 06 '11 at 16:54
  • 1
    RewriteMaps & other methods could be employed for that in Apache, but you're right it would be easier to let PHP handle things. Do close your session (`session_write_close()`) before sending big files though, otherwise the session is locked, and something like [X-Sendfile](http://codeutopia.net/blog/2009/03/06/sending-files-better-apache-mod_xsendfile-and-php/) is also nice. Common practice for protected/limited files is usually to not even have them directly available, i.e.: outside the document root of the site. – Wrikken Jun 06 '11 at 16:57
1

Well, yes: you'll need to implement the caching logic (Expires:, ETag:, Last-Modified:, 304 Not Modified and such), as PHP doesn't do that for you; if you're using sessions, you'll want to play (or fight) with the session cache limiter (as it tends to screw up caching by sending no-cache and needs to be overridden with the correct caching headers). See this for a simple example - it is older but still functional.

While you're at it, implementing 206 Partial Content would also be useful ("resumable downloads").

Community
  • 1
  • 1
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
0

You need to know how you want the browser cache to behave, and set the appropriate headers to be sent back with the results of the request.

Codecraft
  • 8,291
  • 4
  • 28
  • 45