htaccess remove www and https ONLY for main domain (no subdomains) - .htaccess

I'm looking to use htaccess to remove www, and force https ONLY for main domain (no subdomains). I was also hoping to avoid hard coding my domain or any of the subdomains. Here is what I am currently using, but it is forcing https for subdomains.
Thanks in advance,
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Try this :
RewriteEngine on
RewriteCond %{REQUEST_SCHEME}#%{HTTP_HOST} ^http#(?:www\.)?(example\.com)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
Replace example.com with your main domain.

Related

.htaccess redirect www to non-www for subdomain that is before the www

I need to make a request to https://subdomain.www.domain.com rewrite the URL to https://subdomain.domain.com
Which part of the below rewrite do I change to achieve this? I'm guessing I have to put the subdomain in front of the www, but what do I put there, also how do I capture all subdomains - what's the general rule for all subdomains?
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
You may use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.(domain\.com)$ [NC]
RewriteRule ^ https://%1.%2%{REQUEST_URI} [R=301,L,NE]

htaccess quite domains not www to https://www but not subdomains to www

I've found quite examples but all of them for an especific domain, and my server has many main domains and subdomains.
I want all main domains to be rewrite to https://www but the subdomains to https:// only:
main-domain.xxx -> https://www.main-domain.xxx
sub.main-domain.xxx -> https://sub.main-domain.xxx
I'm using now this rules to get https://
RewriteCond %{REQUEST_SCHEME} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I've found this as a question not as an answer but works fine for me:
RewriteCond %{REQUEST_SCHEME} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

how to redirect www and non www to https for specific domain using htaccess

I need to redirect www and non www to https. I have looked everywhere on stackoverflow but can't find quite what I'm looking for.
The rules are:
example.com and www.example.com and https://example.com
must redirect to https://www.example.com
It must match the domain and extension example.com. It cannot be a wild card which also
matches abc.com or 123.com or example.net or anything else
It must match for www subdomains only. It cannot be a wild card which
also matches sub.example.com or thisisademo.example.com
I currently have:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
However if someone enters www.example.com it still goes to the http version instead of https.
What I think I actually need here is RewriteCond regex to match exactly and only "example.com" and "www.example.com"
Thanks
You can use the following rule to redierct non-www or www to https://www in just one redirection
#redirect http non-www to https://www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
#redirect https non-www to www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
Clear your browser's cache before testing this rule.
This is the only way it works for me - with !on instead of off and with %{ENV:HTTPS} instead of %{HTTPS}:
#non-www. http to www. https
RewriteCond %{ENV:HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
#non-www. https to www. https
RewriteCond %{ENV:HTTPS} on
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
#First rewrite any request to the wrong domain to use the correct one (i.e. www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

https for main domain and http for subdomain

So i need very simple codes for .htaccess
I want to redirect all traffic to https in main domain and redirect all traffic to http in subdomain without WWW.
Right now i am using this code to redirect all traffic to https
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
So main domain should be like this: https://website.com
and subdomain should be like this: http://sub.website.com
I found many answers but very complicated, so need simple solution.
EDITED
Solution given by anubhava works great. Thanks for your solution.
You can use:
RewriteEngine On
# maindomain -> https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(website\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R,L]
# subdomain -> http
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(sub\.website\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R,L]

Force HTTPS and WWW for domain and only HTTPS for subdomains HTACESS

I've been trying to configure this for my website, but not being able to.
I use to have a cond on my .htaccess to force www for the main domain and nothing for subdomains, but since I got a SSL, I'm having some problems.
It's a wildcard SSL.
What I need is force HTTPS:// WWW on the main domain, and HTTPS:// on subdomains.
I.E: http://www.domain.com -> https://subdomain.domain.com
Is there any rule for that?
Thanks!
EDITED
Now I'm using like Jon posted
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [NC]
RewriteRule ^.*$ https://www.domain.com.br%{REQUEST_URI} [R,L]
# ---------- #
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com\.br$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.*)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]
The thing is, when on main domain if I type HTTP:// with or without WWW, ir forces HTTPS:// and WWW, that's ok...
But on subdomain, when I type HTTP it doesn't force HTTPS, it redirects to the main domain only... that does not happen if I put a .htaccess inside the dir of the subdomain.
With a .htaccess inside my subdomain dir, if I type HTTP, it forces HTTPS normally...
Any suggestions?
Try:
RewriteEngine On
# for subdomains
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.*)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]
# for main domains
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com%{REQUEST_URI} [R,L]

Resources