Hardcore Apache question: How to have an .htaccess file pull from WordPress?
July 24, 2006 7:56 PM   Subscribe

Hardcore Apache question: How can I write an .htaccess file that will apply a RewriteRule to only the most recent file posted to my Wordpress blog?

I post a video a day on my site, and I need to redirect the traffic to the most recent video to libsyn, which is set up for the traffic patterns caused by podcasts -- otherwise, my server runs sluggishly for a few hours every morning. The WordPress posts are written using a custom field which contains the URL of the video file to be posted, and I can hack WordPress to output a file containing only this URL. How can I write an .htaccess file that will compare the request string to the contents of this file, so that the URL of the most recent video will be automatically overwritten?

Note that I'm not extremely conversant with the details of Apache configuration -- I've modified the regexps of a few RewriteRules that a friend wrote for me, but that's about it -- so the fuller the code sample, the better. Barring that, I'm a quick study, so a link to a syntax reference would do me fine as well.

Thanks!
posted by tweebiscuit to Computers & Internet (13 answers total) 1 user marked this as a favorite
 
Could you hack it to do output some constant strings in the file in addition to the url? If you look at the mod_rewrite page it talks about RewriteMap, which reads an external file (which could be the thing you generate). I'm not positive this will work -- it might only get read when the httpd.conf file is read, which wouldn't be good enough for this.

Barring that, hmm, you may be out of luck with a rewrite solution. I don't see any way to read a file for rewriting purposes. It might be worth looking into writing a small cgi script (in php or whatever language) that people would hit to get the video, and it'd redirect them to the appropriate place. Not as clean, but it'd work.
posted by inkyz at 8:21 PM on July 24, 2006


Correct me if I'm misunderstanding your question, but couldn't you have a php or perl script run after you post the video that writes out a .htaccess which does the redirect you need?
posted by justkevin at 8:30 PM on July 24, 2006


Response by poster: justkevin -- I could, but a) from what I've been told, having a script edit .htaccess is Considered Harmful. b) I'm good enough with PHP to hack my way through Wordpress, but not good enough to write such a script from scratch -- thus my hope of finding a hook in .htaccess that will allow it to read from a file I can easily output. (And yes, I can definitely have it output some constant strings in addition to the filename.)
posted by tweebiscuit at 8:52 PM on July 24, 2006


Response by poster: ikyz, when is the httpd.conf file read? I'm on a shared account, so I don't have access to httpd.conf.
posted by tweebiscuit at 8:53 PM on July 24, 2006


Whoops -- doing more investigation, I see RewriteMap is only available in httpd.conf, not in .htaccess. So I think you're out of luck with RewriteMap, sorry. But it seems like you can take justkevin's suggestion, can't you? Just have your script write out a one-line .htaccess file with the RewriteRule and you should be set.
posted by inkyz at 9:36 PM on July 24, 2006


Response by poster: Hmm. Well, it will take some extra research on my part. Will it be pretty easy to output a single line to a file from WordPress? I'm skeptical about my abilities to write a script from scratch, but if there's a simple way to output a variable to an arbitrary file in the middle of a PHP script I can probably handle that.

A quick Google shows a few too many options to sort through -- what function / method should I be using for file output?
posted by tweebiscuit at 9:44 PM on July 24, 2006


Response by poster: (Also, any other suggestions on ways to do this would be appreciated!)
posted by tweebiscuit at 9:45 PM on July 24, 2006


My first instinct is to look into using the Unix "environment" to pass information to mod_rewrite. Have Wordpress put the most recent post url into an environment variable, and then in mod_rewrite, use the environment variable.

This assumes a lot. PHP should be able to set environment variables, but mod_rewrite may not be able to consume them. Furthermore, environment variables can only (strictly?) pass from parent process to child, and I doubt mod_rewrite is a child process of wordpress — they are most likely either sibling processes or in the same process. If they are in the same process, environment variables may still work.

This line does suggest an alternate mode of thinking when googling, though. Good luck.
posted by clord at 10:10 PM on July 24, 2006


What's the format of the URL? You could use RewriteCond to test it against %{TIME_*} variables and have the RewriteRule conditional on the date specified in the URL being the current date.

For PHP, you want fopen/fwrite/fputs/fclose, however, you'll almost certainly need to give the webserver write permissions on the file, which might not be a great plan.

Other options include using a CGI to update your .htaccess if your provider's using suexec (so no permissions problems), using a cronjob to update it periodically, or having WordPress change the links it generates so it sends users to libsyn conditionally.

Some more details on your workflow in making new posts might help narrow things down.
posted by Freaky at 10:29 PM on July 24, 2006


I would use mod_alias and the Redirect directive, spitting out a new .htaccess file each time you post.

Or you could write a small PHP script that sits in between the user and the podcasts. They'd fetch them by going to http://servername/getpodcast.php?file=file.ogg, which would check the modification times on the files, and if the requested file was the last updated, the script would redirect them to libsyn.
posted by beerbajay at 4:21 AM on July 25, 2006


Response by poster: Thanks, Freaky and beerbajay -- I'll look into those options.

Our workflow is as follows: We FTP the video to a directory on our site, with a URL like this:

http://www.oldeenglish.org/videos/OldeEnglish.org_-_Name_Of_Video.mov

We then also upload the video to Libsyn, which gives it a URL along these lines:

http://media.libsyn.com/media/aconover/OldeEnglish.org_-_Name_Of_Video.mov

We then link to that video in a WordPress post -- current we manually make a Quicktime embed for every post (via a WP plugin that does most of the heavy HTML lifting), but for these purposes I could change our system to use a Wordpress custom field and put the embed code in the template, since it doesn't change post to post.

However, that URL is used on quite a few templates, and of course we link to the videos outside of WordPress as well, so a an .htaccess solution would be the best in my mind, since it's the most universal possible.

The PHP script isn't really a possibility -- our site has been up for years, and our URLs are well known to our audience, so I'd rather not filter them through a PHP file if I can avoid it. Also, certain podcast aggregators (cough*itunes*cough) tend to balk at media files served through scripts.
posted by tweebiscuit at 12:06 PM on July 25, 2006


You don't need to pass the file through the php script, just use the php script to redirect to the actual file location (local if it's old, libsyn if it's new).

I don't really know how the podcast aggregators would react to redirects, though... not really my area of expertise.
posted by beerbajay at 12:52 PM on July 25, 2006


You could modify the .htaccess every time you upload (something you could automate with a local script) and simply do:

RedirectTemp /video/OldeEnglish.org_-_Name_Of_Video.mov http://media.libsyn.com/media/aconover/OldeEnglish.org_-_Name_Of_Video.mov

You could also map the URLs to a PHP script that performs redirects based on some logic and file modification times, but that might be a bit over the top; it would potentially allow more flexibility without invalidating your current URL scheme, but it would mean either a lot more redirects or writing half a webserver to serve files directly from PHP properly.
posted by Freaky at 6:35 PM on July 25, 2006


« Older 3rd gen iPod woes   |   Apache/Linux permissions issue Newer »
This thread is closed to new comments.