0

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

1 Answers1

3

What comes in mind would be to use mod_rewrite.

Something like this :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example.com$
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

However, note that Apache recommends the use of the Redirect directive in the Virtual Host configuration to achieve this.

From the doc When to not use mod_rewrite :

In the case of the http-to-https redirection, the use of RewriteRule would be appropriate if you don't have access to the main server configuration file, and are obliged to perform this task in a .htaccess file instead.

krisFR
  • 12,830
  • 3
  • 31
  • 40