Apache URL rewriting with ServerAlias

apache-2.4rewriteserveraliasvirtualhost

I have a vhost running on a CentOS 7 Server, which is serving 2 Prestashop stores.
In this vhost conf file, I have a ServerName and a ServerAlias, each directing to a dedicated store.

Recently I moved both stores to HTTPS, but one question remains: I know how to rewrite the URL to redirect from HTTP to HTTPS, but can I redirect based on the URL asked by the client?

I know how to do it with 2 vhosts, but as the conf will be near identical, i wanted to do it with only one file.

Example: rewrite http://store1.example.com to https://store1.example.com AND http://store2.example.com to https://store2.example.com all in the same Vhost conf file.

Best Answer

You can just use the HTTP_HOST variable that apache sets:

<VirtualHost *:80>
  ServerName store1.example.com
  ServerAlias store2.example.com
  RewriteEngine On
  RewriteRule ^/?(.*)$ https://%{HTTP_HOST}/$1 [R=301]
</VirtualHost>
Related Topic