Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect all subdomains from http to https

Tags:

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?

like image 345
Super Engineer Avatar asked Apr 14 '14 11:04

Super Engineer


People also ask

How do I redirect a subdomain to https?

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) .

Can you redirect subdomain?

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.


1 Answers

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

like image 82
arco444 Avatar answered Sep 20 '22 05:09

arco444