Main Content

Adding a passcode to a directory

Archive - Originally posted on "The Horse's Mouth" - 2012-06-05 08:00:34 - Graham Ellis

Scenario - I wanted to provide an easy download link that can be used to circulate a number of documents to people on a maiing list, with them able to pick and choose which they read. Don't want them to have to login / give passwords, but do want to be able to make it something more than just a simple URL. Don't want to have to write much code either.

Simple solution - require them to add a piece of text as the query string for each item downloaded; if sending out links in am email, that extra text can go into the link. Use mod_rewrite to check for the query string that's required. If it's there, feed the required document, otherwise feed an error page.

Here's my .htaccess file (with the passcode - extra string - altered from the real thing, of course!):

  RewriteEngine Off
  RewriteEngine On
  
  RewriteCond %{QUERY_STRING} (10_per_day)
  RewriteRule ^(.*)$ [L]
  
  RewriteRule ^(.*)$ i.txt


The i.txt file is my error text file; the passcode can be easily changed and since it's a regular expression I can do all sorts of things with it like add alternatives. It would also be a good idea to add this directory to my robots.txt just in case someone publishes a link and passcode on their own page.

P.S. The rather curious off and on for the engine at the start is to prevent the parent directory's mod_rewrite settings percollating down to this directory.