4

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

Artem Razin
  • 143
  • 5
  • 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 https://stackoverflow.com/questions/166607/how-do-i-find-the-version-of-apache-running-without-access-to-the-command-line just shows me that it's Apache but suddenly without version. – Artem Razin Dec 12 '18 at 14:38

1 Answers1

3

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?

MrWhite
  • 12,647
  • 4
  • 29
  • 41