2

I use a separate file software_version that is included using SSI into html files. So visitors can view current software version on download and some other pages.

Also there is an URL to download the most current version of software:

redirect /download/MySoftwareSetup.exe https://download.somedomain.com/MySoftwareSetup_2018.1.0.0.exe

I have a script that updates the file software_version, and that HTML files display correct version.

But I have to manually edit .htaccess file to make redirection to the freshest build work correctly.

Can .htaccess read some variable from an external file and substitute it while this file is being processed? Something like the following:

redirect /download/MySoftwareSetup.exe https://download.somedomain.com/MySoftwareSetup_${file:software_version}.exe

  • What version of Apache are you using? – MrWhite Dec 12 '18 at 11:20
  • @MrWhite it's a shared hosting, and I couldn't get this information. this method stackoverflow.com/questions/166607/… just shows me that it's Apache but suddenly without version. – Artem Razin Dec 12 '18 at 14:38
2

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.