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.

2

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>
0

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>
  • I admit I didn't think of this way to do it. Thanks for your answer! – IsKor Mar 2 '18 at 13:48

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.