0

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.

Andrew Schulman
  • 8,561
  • 21
  • 31
  • 47
IsKor
  • 45
  • 3
  • 10

2 Answers2

3

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>
Andreas Rogge
  • 2,670
  • 10
  • 24
1

You can put them in one or many files as you like, but the most straightfoward way to do this is with multiple <VirtualHost> directives:

<VirtualHost *:80>
    ServerName store1.example.com
    Redirect permanent / https://store1.example.com
</VirtualHost>
<VirtualHost *:80>
    ServerName store2.example.com
    Redirect permanent / https://store2.example.com
</VirtualHost>
<VirtualHost *:443>
    ServerName store1.example.com
    ServerAlias store2.example.com
    ...
</VirtualHost>
Andrew Schulman
  • 8,561
  • 21
  • 31
  • 47