blog.humaneguitarist.org

tweaking Simple Download Manager, a Wordpress plugin

[Sun, 12 Jun 2011 14:25:54 +0000]
I wanted a way to track downloads available through my blog without having to do a lot of work and without having to check server access logs. So I installed Simple Download Manager [http://wordpress.org/extend/plugins/simple-download-monitor/] (v. 0.21). It's great. I did do one thing a little differently than the instructions in regard to editing my .htaccess file. Here are the instructions [http://wordpress.org/extend/plugins/simple-download-monitor/installation/]: The last step involves editing the '.htaccess' file. The default '.htaccess' skips default WordPress processing for existing files, which means that direct-linked files would get downloaded directly, without Simple Download Monitor ever learning about it. You need to modify the '.htaccess' file so that downloads are passed through Simple Download Monitor. This is easy enough to do: Open your '.htaccess' file and locate line RewriteCond %{REQUEST_FILENAME} !-f Add this line directly above it: RewriteRule ^(files/.*) /index.php?sdmon=$1 [L] (replace 'files/' with your download directory). Other than changing the "files" folder to "uploads", I altered the line to read like this: RewriteRule ^(uploads/.*\..*) /index.php?sdmon=$1 [L]<br/> The reason for this is that using the default line would have prevented users from directly browsing directories through their browser. And that would have been a problem since with some projects I provide a link to a directory so people can download past versions of software. For example: [DEL: Click here to download the latest Windows self-installer. :DEL] [DEL: Click here to download the latest ZIP file. :DEL] Older versions can be accessed here [http://blog.humaneguitarist.org/uploads/PubMed2XL]. Simple Download Manager will still track downloads for the types of files I've asked it to track whether the user downloads the file via clicking a link in a blog post or by downloading the file from the directory. That's exactly what I wanted. I'm pretty lousy with regular expressions, so for my own notes here goes ... There are three parts, as I see it, to this regex: .*\..* Those parts are: 1. .*<br/> 2. \.<br/> 3. .*<br/> The first matches anything, the second makes sure the match ends with a dot, and the third means the match can have anything after the dot. This matches filenames a la "filename.ext" but excludes directories. By the way, this means I cannot name directories with a dot in the directory name, otherwise the RewriteRule will kick in on the directory - which won't work and the user won't be able to traverse that directory. I guess I should learn the regex to allow me to create directories with dots in the directory name ... :/ Update, June 12, 2011: Actually, I've since reconsidered. I only want to track ZIP and EXE downloads. Doing otherwise would force users to have to download other file types like text and XML files. That's not a user-friendly approach. So, the addition to the .htaccess file now reads: RewriteRule ^(uploads/.*\.zip) /index.php?sdmon=$1 [L]<br/> RewriteRule ^(uploads/.*\.exe) /index.php?sdmon=$1 [L]<br/> The two lines ensure that only ZIP and EXE files are processed by Simple Download Manager ... and I can now make directory names with dots in them. Update, November 26, 2011: I need to mention that this addition to the .htaccess file should not go in the WordPress block otherwise it will get periodically overwritten, preventing the download manager from working. It maybe better to just see an example ... # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress #Notes: don't put anything in the WordPress block per: http://www.webpronews.com/keep-wordpress-from-overwriting-custom-htaccess-rules-2007-06 #Simple Download Manager RewriteRule ^(uploads/.*\.zip) /index.php?sdmon=$1 [L] RewriteRule ^(uploads/.*\.exe) /index.php?sdmon=$1 [L]