0

Providing I have 1 Ubuntu server which holds two sites (they are subdomains and also they hold code for other subdomains):

  • house.example.com (also I have api.example.com) - production site
  • house-dev.example.com (also have api-dev.example.com) - development site

How could I redirect all http request for house-dev.example.com to https://house-dev.example.com and all http request for api-dev.example.com to https://api-dev.example.com?

My conf files in sites-enabled are:

For house.example.com

<VirtualHost *:80>
    ServerName example.com
    ServerAlias house.example.com api.example.com
    (...)
</VirtualHost>

<VirtualHost *:443>
    (...)
</VirtualHost>

For house-dev.example.com:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias house-dev.example.com api-dev.example.com
    Redirect "/" "https://house-dev.example.com"
    (...)
</VirtualHost>

<VirtualHost *:443>
    (...)
</VirtualHost>

How could I redirect all http traffic of house-dev (and associated api-dev) to https?

I have all .crt and .key files in place and paths are all correct. I am on about the Redirect method.

I have tried Redirect "/" "https://house-dev.example.com" and it works fine for house-dev, but when I try http://api-dev.example.com the redirection goes to https://house-dev.example.com.

0

I have tried Redirect "/" "https://house-dev.example.com" and it works fine for house-dev, but when I try http://api-dev.example.com the redirection goes to https://house-dev.example.com.

Of course it does because both hostnames are handled by the same virtual host, and you have a fixed redirect to house-dev.example.com.

A simple fix for this would be to just create a separate port 80 vhost for each hostname, and have the correct redirect entry in each.

Alternatively you could use RewriteRule to perform to redirect. Something like the following (untested)

RewriteEngine on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,QSA,L]

It's also apparently possible to use variables with Redirect, if you use it inside a location block. The following comes from https://stackoverflow.com/questions/40289269/using-redirectmatch-with-http-host-in-the-destination

<Location "/">
    Redirect "https://%{HTTP_HOST}%{REQUEST_URI}"
</Location>
| improve this answer | |

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.