htaccess redirect downloads to latest version

In the following segment, I cover using htaccess to redirect file downloads to the latest version of the file. Here you'll learn how to create a rewrite rule for redirecting all file download requests for earlier file versions to the latest revision. For example all files from filename-0.0.0.1.exe to filename-2.0.0.9.exe should be redirected to filename-2.0.0.9.exe

How to use htaccess to redirect downloads

htaccess to redirect downloads

To redirect all file downloads to the latest file revision using htaccess, you can use a RewriteRule or RedirectMatch. Both methods are being covered here for your convenience.

Note: The following assumes your downloadable files are located in the yourdomain.tld/downloads/ directory. If not, replace /downloads/ with the path to your files. You also need to replace filename- with the actual name of your downloadable file. Also note the revision number is set to 4 places filename-x.x.x.x. Adjust the number of digits to fit your particular situation.

Redirect downloads using a RewriteRule

To redirect all download requests for an earlier filename to the latest version using a RewriteRule, use the following code in your htaccess file.

RewriteEngine On
RewriteBase /downloads/
RewriteRule ^filename-(\d+\.\d+\.\d+\.\d+)\.exe$ filename-2.0.0.9.exe [R=307,L]

This rewrite rule uses regular expressions to match any file download requests for earlier versions of the filename (from filename-0.0.0.0 to filename-2.0.0.9.exe), and redirects them to the latest revision which in this case is filename-2.0.0.9.exe. Specifically, the regular expression ^filename-(\d+\.\d+\.\d+\.\d+)\.exe$ matches any file name that starts with "filename-", followed by four sets of digits separated by periods, and ending with ".exe". The four sets of digits represent the version number of the file.

The (\d+\.\d+\.\d+\.\d+) part of the regular expression captures the version number as a group, which we can then reference in the replacement string using the backreference $1. In the replacement string, we simply replace the captured version number with the latest version number "2.0.0.9".

The [R=307,L] flags at the end of the rule indicate that we want to send a 307 redirect status code to the client (which tells search engines and browsers that the requested page has moved temporarily and might be back), and that we want to stop processing any further rewrite rules (since we've already found a match).

Redirect Downloads using RedirectMatch

To use RedirectMatch to redirect all download requests for an earlier filename to the latest version, you can use the following code anywhere after RewriteEngine On in your htaccess file.

RedirectMatch 307 ^/downloads/filename-\d+\.\d+\.\d+\.\d+\.exe$ /downloads/filename-2.0.0.9.exe

This RedirectMatch directive matches yourdomain.tld/downloads/filename-, followed by four sets of digits separated by periods, and ending with .exe. The \d+ regular expression matches one or more digits, and the ^ and $ anchors match the beginning and end of the URL, respectively. When a match is found, the user will be redirected to the latest revision of the filename-2.0.0.9.exe file using a temporary 307 redirect.