How do you redirect only the HTTP subdomain to HTTPS subdomain in .htaccess
? The main site is in WordPress and already redirected to HTTPS with a plugin, but the subdomain is a PHP created site. I have looked and not seen a conclusive solution to this on here.
I copy and pasted this suggested code but it did nothing:
#Redirect Subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
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) .
If you are using the popular Apache Web server, you can easily redirect all traffic from unsecured HTTP to HTTPS. When a visitor goes to your site will be redirected to the secure HTTPS protocol. The server must allow you to use module mod_rewrite, but it's not a problem for most webhosting providers.
RewriteEngine on RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC] RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This would create a redirect loop (if executed at all). 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) .htaccess
file if the subdomain points to the document root of the main site. The important thing is that the redirect must go before the WordPress front-controller.
This also assumes your SSL cert is installed directly on your application server and not a proxy.
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This checks that HTTPS
does not contain "on" and the subdomain is being requested before redirecting to HTTPS on the same host.
Although if WP is simply redirecting the main domain, then you could do all this in .htaccess
and simply remove the condition that checks against HTTP_HOST
. Although if you have other subdomains that should not be redirected to HTTPS then alter the CondPattern to match just the subdomain or main domain (and www subdomain). For example:
RewriteCond %{HTTP_HOST} ^((subdomain|www)\.)?example\.com [NC]
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