redirect all subdomains from http to https

redirect subdomain to https using htaccess
how to redirect http to https
apache redirect http to https
redirect to https
apache redirect subdomain to https
redirect to https vhost
subdomain https htaccess
apache redirect www to https

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

<VirtualHost *:80>
  Redirect 302 /

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

For example should go to and should go to

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


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

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

<VirtualHost *:80>
  ServerAlias *

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

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

redirect all subdomains from http to https, What comes in mind would be to use mod_rewrite. Something like this : RewriteEngine On RewriteCond %{HTTP_HOST} ^(.*)\$  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.

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]


Redirect all HTTP traffic to HTTPS including subdomains, If this is a reasonably recent Apache. This ability can be achieved in extra/http-​vhosts.conf . But your ability to accomplish the following in  The second step involves htaccess redirect to https in Apache or NGINX. Insert this code to the.htaccess file and force all web traffic to use HTTPS. RewriteEngine On RewriteCond % {SERVER_PORT}80 RewriteRule ^ (.*)$$1 [R,L] Don’t worry if you don’t know how to access the.htaccess file, keep reading.

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>
    ServerAlias *

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

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

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.

Redirecting http subdomain to https subdomain, Redirecting http subdomain to https subdomain. Specify port 80 not any port containing 80 as digits in the port number. Specify using No Case. If both conditions are met, redirect the {REQUEST_URI} to the secure server without duplicating Apache variables for {HTTP_HOST} and {REQUEST_URI}; any query If you are tracking data in Google Analytics, a user who redirects from an HTTPS site to an HTTP site (by clicking links or redirects, etc.), it’s been shown that that referral data is lost. Traffic from HTTPS to HTTP usually ends up getting lumped together with “direct traffic.” HTTPS to HTTPS is counted as referral data.

How to use .htaccess to Force HTTPS in Cpanel, Redirect to/from a subdomain; Redirect from http:// to https:// or https://www having In terms of HTTP-HTTPS redirect, all site requests are split between  Since HTTPS is now a standard for all websites, it’s become more important than ever to redirect traffic to the HTTPS version of a website. Recently, browsers like Google Chrome are showing “nonsecure” badges for websites still running over HTTP.

htaccess force HTTPS to main domain and all subdomains , The HTTP to HTTPS redirect directives as stated should work just fine if used in the subdomain's .htaccess file. If I install the plugin and try to  You can use the code below to redirect a subdomain and you will have to place the code as default document on the document root. While applying the above code, be sure to replace the with your real subdomain URL, replace the /subdomain with the real name of your subdirectory and in the last piece, note that you will be redirecting to a given file.

Redirect from www and http to https doesn't work on sub-domains , I would implement it as two rule sets. First I would redirect any www URL then in a second rule I would redirect HTTP. RewriteEngine On  HTTP to HTTPS redirect isn’t implemented across all pages on your site (e.g., subdomains). HTTPS to HTTP redirects To be sure there are no such issues, run a crawl with Ahrefs’ Site Audit .