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?
In order to redirect from HTTP to HTTPS you need to first check you are not already on HTTPS (otherwise you will get a redirect loop). These directives would need to go in the . htaccess in the document root of your subdomain. Or at the top of your root (WordPress) .
Under Modify a Subdomain, locate the domain you want to redirect, then click its Manage Redirection link on the right. In the text box, type the URL you would like visitors to be redirected to if they go to the subdomain sample1.hgexample.com. Click Save. You will see the redirected URL under the Redirection column.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With