3

Currently, I have a website (example.com). http to https redirect is working via my .htaccess file

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

This website also runs on subdomains.

If someone goes to www.subdomain.example.com of the website, I want them to be redirected to https://subdomain.example.com.

Is it possible to achieve this with the htaccess file without impacting the http to https redirect that I have working for example.com?

Thanks!

Jason Howard
  • 1,464
  • 1
  • 14
  • 43
  • Possible duplicate of [apache redirect http to https and www to non www](https://stackoverflow.com/questions/9945655/apache-redirect-http-to-https-and-www-to-non-www) – Dusan Bajic May 28 '19 at 07:46

1 Answers1

2

Yes, you can add multiple conditions.

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.subdomain\.example\.com$ [NC]
RewriteRule ^(.*)$ https://subdomain.example.com/$1 [R=301,L]

RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

1: If the HOST is www.subdomain.example.com, redirect to https://subdomain.example.com/

2: If its not https, redirect to https.

filipe
  • 1,957
  • 1
  • 10
  • 23
  • how can I prevent the subdomain and domain from being hard coded? My site will run multiple domains and multiple subdomains for each site. – Jason Howard Aug 28 '19 at 06:26