how to make htaccess force https for www.example.com and example.com but no other subdomains - apache

I have a host that has many subdomains and I want to force https (redirect for https ) for www.example.com and example.com but not any other subdomain, how to do this?
currently I am using :
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
but this does not work as expected

You can just add a condition using HTTP_HOST:
RewriteCond %{HTTPS} !=on
RewriteCond HTTP_HOST ^(?:www\.)?example\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

Related

redirect all non-www to www, all http to https, and exclude subdomains

I am trying to redirect
example.com to www.example.com
and
http://example.come to https://www.example.com
HOWEVER, if a user types in
admin.example.com, http://admin.example.com or https://admin.example.com
it should NOT redirect to www.admin.example.com
What .htaccess can I use for this?
You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+\.[^.]+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Regex pattern (?:www\.)?([^.]+\.[^.]+) will match example.com or www.example.com but not admin.example.com

How to prevent subdomains from redirecting to https - htaccess?

I am trying to work out how to redirect all http instances to https:// though excluding the subdomain.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?example.co [NC]
RewriteRule (.*) https://example.co/$1 [L,R=301,QSA]
For example, domain.example.co should be left as it is and not redirected to https://.
example.co or example.co/sub/sub should be redirected to https://
I have tried changing the RewriteRule:
RewriteRule ^$ https://example.co/$1 [R,L]
This leaves subdomains as they are but has no effect on subdirectories under example.co - i.e. example.co/sub/sub.
How could I redirect from http to https but exclude all subdomains?
Note
I also have a rewrite rule which points subdomains to their directories without changing the URL:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
sub.example.co will displays example.co/sub but the URL will not change
You can try this :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(.+\.)?domain\.com$
RewriteRule ^(.*)$ https://%1domain.com/$1 [R,L]
Or This :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R,L]
Note: Replace sub with subdomain and domain with domain name
Enjoy it ;-)
Try below I am assuming you are not using www appended to host.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^example.co [NC]
RewriteRule ^(.*)$ https://example.co/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
ProxyPassReverse / http://example.co/

htaccess force https & www to non-www, but subdomain only https

I have a two websites
yourdomain.com
reseller.yourdomain.com
Now I want to forcing both domains to use https via apache htaccess redirect. In addition to that, the main domain should use always www instead of non-www. But the subdomain reseller.yourdomain.com shouldn't use www of course.
How should the redirect look like? I have tried so many things like
RewriteEngine on
RewriteCond %{HTTPS} !^on
RewriteCond %{HTTP_HOST} !^reseller\.yourdomain\.com [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This works not 100%, the main domain redirects correctly to www and ssl, the ssl-redirect for the subomdain works only on reseller.yourdomain.com, not on sub-sites like reseller.yourdomain.com/single-page.
thanks!
Try it like this,
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^yourdomain.com
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.+)\.yourdomain.com$
RewriteRule ^ https://%1.yourdomain.com [R=301,L]

All http traffic to domain.com and all https to sub.domain.com with htaccess

I have two domain names www.domain.com and sub.domain.com, and I want to redirect all the http traffic to www.domain.com and all the https traffic to sub.domain.com. For example, if the url called is http:// sub.domain.com/index.php, the server have to rewrite the url to http:// www.domain.com/index.php, and if the url called is https:// www.domain.com/index.php, the server have to rewrite the url to https:// sub.domain.com.
I have this code in htaccess but it does not work correctly:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^/?$ "http\:\/\/sub\.domain\.com\/" [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\/" [R=301,L]
It redirects correctly the https to sub.domain.com but only when there is no page indicated (https:// domain.com/index.php/some-page not work and https:// domain.com yes). For the http not work at all.
Thanks in advance!
You can put this code in your root htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Make sure both domain.com and sub.domain.com share the same document root folder. Otherwise, you will have to split htaccess code in two htaccess (one per root domain folder)
/domain/root/folder/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R=301,L]
/subdomain/root/folder/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

how to redirect everything to SSL https except a few urls

In my htaccess i use the following rule to redirect everything under SSL,
now i need to add a few exception, for example
http://www.mysite/blog-feed.html must be reachable for the http protocol.
#ALWAYS REDIRECT TO SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can just try:
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/blog-feed.html
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]