Boxygen
Boxygen

Reputation: 31

How to Write .htaccess that handles subdomains also to redirect to https:

I have this .htaccess rule

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

This covers the parent domain but not the subdomain.

My subdomain has the same public folder as of parent domain.

example.com is redirected to https://example.com

But subdomain.example.com is not redirected to https://subdomain.example.com

As I said above that both points to same public folder so I want the above rule to handle both.

But I don't want to write my domain or subdomain name in htaccess rule. It shall cover any domain and any subdomain

Upvotes: 0

Views: 108

Answers (2)

Tarik Billa
Tarik Billa

Reputation: 133

  1. Goto your subdomain folder
  2. Create .htaccess file in this folder
  3. and paste this code:

just copy and paste this code:

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

Upvotes: 0

user2493235
user2493235

Reputation:

You can do this with the HTTP_HOST header:

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

So you get it from the request rather than using the server name for the virtual host. Also simplifying the regex since you’re not using the capture.

Upvotes: 1

Related Questions