HTTP subdomain to HTTPS subdomain in htaccess and http to https redirect

HTTP subdomain to HTTPS subdomain in htaccess

HTTP subdomain to HTTPS subdomain in htaccess – How do you redirect only the HTTP subdomain to the 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 here. google technical seo best use seo-for-beginners

Force domain to HTTPS but set subdomain to HTTP

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

 

RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

These directives would need to go 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.

HTTP subdomain to HTTPS subdomain in htaccess – 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]

To first force HTTPS, you must check the correct environment variable %{HTTPS} off, but your rule above then prepends the www. Since you have a second rule to enforce www., don’t use it in the first rule.

RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

 

About proxying HTTP subdomain to HTTPS subdomain in htaccess

When behind some forms of proxying, whereby the client is connecting via HTTPS to a proxy, load balancer, Passenger application, etc., the %{HTTPS} variable may never be on and cause a rewrite loop. This is because your application is actually receiving plain HTTP traffic even though the client and the proxy/load balancer are using HTTPS. In these cases, check the header instead of the variable. This answer shows the appropriate process

BAD SOLUTION AND WHY!

Don’t ever use the solution below because when you are using their code that is something like:

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]

The browser goes to:

http://example.com

Then redirects to:

https://example.com

Then redirects to:

https://www.example.com

This is too much request to the server.

Most of the answers even accepted one has this problem.


https to https redirect BEST SOLUTION AND THE ANSWER

https to https redirect – This code has an [OR] condition to prevent dual changes at URL!

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]

 

HTTP subdomain to HTTPS subdomain in htaccess – A subdomain is defined as a domain that is a part or an extension of a main domain. Subdomain’s can be anything from the commonly used www to something very specific such as docs.google.com. Generally you’d define where a subdomain points or uses as its web root by setting its virtual host entry accordingly, however in some cases you may find that you simply need to redirect your subdomain to a new URL. The focus of this article is to show you how to redirect your subdomain to a different URL using a .htaccess file. If you have not created a .htaccess file or you’re not sure how to edit a .htaccess you can learn how to do both using the following article: Creating and editing an .htaccess file

How To Redirect A Subdomain With .Htaccess

A common misconception is that a subdomain is somehow completely different than the main domain. This however is simply not true, and redirecting a subdomain is almost identical to redirecting the main nonsubdomain. To redirect a subdomain you can modify the following example code to fit your specific needs:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.yourdomain.tld$ [NC]
RewriteRule ^(.*)$ https://www.anotherdomain.tld%{REQUEST_URI} [R=301,NC,L,QSA]

With this code in place in your .htaccess file, the subdomain should now redirect accordingly. If you’re interested in learning how to redirect a main/primary domain please feel free to check out our article that will show you how to accomplish this: Set a permanent (301) redirect using .htaccess. free and premium SEO

Leave a Comment

Lets Talk Call Back

Please enable JavaScript in your browser to complete this form.