27

I redirect all http requests for my subdomain to https by using following code.

<VirtualHost *:80>
  ServerName subdomain.example.com
  Redirect 302 / https://subdomain.example.com
</VirtualHost>

Now my problem is how do I do it for all subdomains.

For example http:subdomain1.example.com should go to https:subdomain1.example.com and http:subdomain2.example.com should go to https:subdomain2.example.com

How do I do it for all subdomains without having to create one virtualhost for all of them

Update

I found that RedirectMatch takes regular expression. Does anyone know how to do it using regex?

Super Engineer
  • 1,966
  • 1
  • 13
  • 21

3 Answers3

31

You could add this to your server's .conf file:

<VirtualHost *:80>
  ServerName subdomain.example.com
  ServerAlias *.example.com

  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
  RewriteRule ^(.*)$ https://%1.example.com$1 [R=302,L]
</VirtualHost>

The ServerAlias will allow the vhost to act as a wildcard, then you can extract the subdomain(s) from the host header and include them in the rewrite to https

rocheio
  • 23
  • 5
arco444
  • 22,002
  • 12
  • 63
  • 67
  • this is a theorical syntax, but it cannot works without a signed certificate. – drabo2005 Apr 14 '14 at 12:08
  • Thanks for the answer. I think this will work for me. But is there an easier syntax for doing this like "Redirect 302" – Super Engineer Apr 14 '14 at 12:09
  • 4
    @drabo2005 - What? This is a VirtualHost on port 80 - i.e. it is only serving HTTP - no certificate is required on this host... – arco444 Apr 14 '14 at 12:10
  • Certificate is required but not in this virtualhost. – Super Engineer Apr 14 '14 at 12:12
  • 1
    @SuperEngineer - I don't think you can use `Redirect` or `RedirectMatch` as they can only operate on the URL path, therefore you won't be able to query the host header – arco444 Apr 14 '14 at 12:16
  • 2
    Your line: `RewriteCond %{HTTP_HOST} ^(.+)\.subdomain\.com$` should be `RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$`. – crunk1 Aug 03 '16 at 08:17
  • Note that you don't really need `RewriteCond`. Domain matching is covered by `ServerName` and `ServerAlias`. Any other domains can and should have a separate VH. – Nux Jul 12 '21 at 19:28
23

Here is a simpler universal modification to the .conf file:

<VirtualHost *:80>
    #...whatever you already have set up...

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

</VirtualHost>
TimmyG
  • 711
  • 7
  • 5
18

As the Apache Wiki entry for RewriteHTTPToHTTPS states,

Using mod_rewrite to do this isn't the recommended behavior. See RedirectSSL

The vhost configuration for forced HTTP to HTTPS redirection - that also works with subdomains - is this:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com

    <Location "/">
        Redirect permanent "https://%{HTTP_HOST}%{REQUEST_URI}"
    </Location>
</VirtualHost>

<VirtualHost *:443>
    [...your vHost configuration...]
    SSLEngine On
    SSLCertificateFile /path/to/your/cert.pem
    SSLCertificateKeyFile /path/to/your/privkey.pem
</VirtualHost>

Explanation: Redirect and RedirectMatch normally don't have the variables (like {HTTP_HOST}) from Mod_Rewrite but if you use <Location > these Variables will be assigned.

Redirect permanent (Alternative: Redirect 301) will redirect with a http 301 code, because

The 301 redirect is considered a best practice for upgrading users from HTTP to HTTPS.

Note: This config is based on Wildcard Certificates for subdomains.

Mischa
  • 693
  • 8
  • 15
  • This does not work. URL to redirect to is missing, although I also believe that the rewrite is the wrong way to do as per your quotation and my research. – Paulo Neves Sep 15 '19 at 12:06
  • 1
    @PauloNeves that might have different causes, a wrong vhost configuration or outdated apache that doesn't support the directive. Try to set loglevel to debug to find out more: https://httpd.apache.org/docs/2.4/mod/core.html#loglevel – Mischa Sep 18 '19 at 16:30
  • @PauloNeves This solution worked in my hands. – András Aszódi May 18 '21 at 13:56