09/01/2018
Updated: Sep 30, 2018 5:20 PM

Site versioning with .htaccess

A small note on how on the Apache server to organize the versioning of a static site using the .htaccess file.

Implementing a versioning of a static site through .htaccess may interest those who use hosting services. There are few options for configuring the server and this is one of them.

It consists of the following:

  1. At the root of the site we put the folder with the name of the version, for example 1.2.0;
  2. Also, put the .htaccess file in the root of the site, with the following directives:
    RewriteCond %{REQUEST_URI} !^/1.2.0/
    RewriteRule ^(.*)$ /1.2.0/$1 [L]

Now the Apache server will redirect all requests without the version number to the specified folder, that is, 1.2.0.

This configuration makes it easy to release new versions, or, if something went wrong, roll back to the previous stable version. It's enough just to change the version in the .htaccess file.

Nuances:
  • All pages will have a duplicate URL: /some/page/url and /1.2.0/some/page/url. For search engines, you need to specify a "canonical page" on all pages:
    <link rel="canonical" href="https://vadimfedorov.ru/some/page/url/" />
  • If you have access to the server, it's probably easier to organize this kind of versioning through "symlinks". Picked up the theme on Stack Overflow.
Illustrations:
At the root of the site we put the folder with the name of the version, for example 1.2.0
At the root of the site we put the folder with the name of the version, for example 1.2.0
Also, put the .htaccess file in the root of the site, with the following directives
Also, put the .htaccess file in the root of the site, with the following directives