redirect all subdomains from http to https
Asked Answered
Z

3

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?

Zygapophysis answered 14/4, 2014 at 11:55 Comment(0)
L
32

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

Lattimore answered 14/4, 2014 at 12:4 Comment(7)
this is a theorical syntax, but it cannot works without a signed certificate.Emmittemmons
Thanks for the answer. I think this will work for me. But is there an easier syntax for doing this like "Redirect 302"Zygapophysis
@Emmittemmons - What? This is a VirtualHost on port 80 - i.e. it is only serving HTTP - no certificate is required on this host...Lattimore
Certificate is required but not in this virtualhost.Zygapophysis
@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 headerLattimore
Your line: RewriteCond %{HTTP_HOST} ^(.+)\.subdomain\.com$ should be RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$.Asclepiadaceous
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.Resoluble
W
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>
Watanabe answered 14/1, 2019 at 0:40 Comment(3)
Simpler and perfect!Levine
This indeed solved my problem bigtime! Testimonial here.Pedersen
Thank You! Thank You! This just saved a novice Linux admin a lot of time and searching!Bixby
U
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.

Uintathere answered 2/7, 2019 at 18:31 Comment(3)
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.Recrement
@PauloNeves that might have different causes, a wrong vhost configuration or outdated apache that doesn't support the <Location> directive. Try to set loglevel to debug to find out more: httpd.apache.org/docs/2.4/mod/core.html#loglevelUintathere
@PauloNeves This solution worked in my hands.Palladio

© 2022 - 2024 — McMap. All rights reserved.