Can .htaccess file read "variable" from external file?
On Apache 2.4 you can use an Apache Expression in a mod_rewrite RewriteCond
directive to read the value from the file and use this as part of the redirect target.
For example:
Assuming the file software_version
is also located in the /download
subdirectory and contains a string of the form 2018.1.0.0
representing the version string, then in your root .htaccess
file you could do something like the following:
RewriteEngine On
RewriteCond expr "file('%{DOCUMENT_ROOT}/download/software_version') =~ /([\d.]+)/"
RewriteRule ^download/(MySoftwareSetup)\.exe$ https://download.somedomain.com/$1_%1.exe [R,L]
The above would externally redirect a request of the form /download/MySoftwareSetup.exe
to https://download.somedomain.com/MySoftwareSetup_2018.1.0.0.exe
(using whatever version string is read from the external file).
The $1
backreference simply holds the "MySoftwareSetup" string from the requested URL to save repetition.
The regex /([\d.]+)/
captures just the version string from the read file. So, even if there are extraneous characters (such as newlines) then these will not be captured. This is stored in the %1
backreference and used in the RewriteRule
substitution string to construct the filename.
I use a separate file
software_version
that is included using SSI into html files.
Although if you are using SSI already then you can presumably modify the HTML anchor in the same way to link directly to the correctly versioned file?