redirect SSL subdomain to non SSL subdomain - .htaccess

I'm completely lost as I've tried a variety of ways to get this accomplished, but nothing seems to work.
All I want to do is get the following setup:
FROM: https://sites.example.com
TO: http://sites.example.com
NOTE: same "sites" subdomain, except I do not want the subdomain "sites" to have https://
I also do want to keep my SSL for all other areas except the "sites" subdomain.
Thanks in advance,
JR

You can use:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^sites\. [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Related

.htaccess - redirect from old domain to new and force HTTPS

I'm having trouble getting these redirects to work under all conditions. I'm hoping I can fix it with .htaccess but there may be something mucked up with the way I'd previously tried to force redirects through my host's control panel.
Anyway, olddomain.com/whatever, with http:// or https:// and with or without www should permanently redirect to https://www.newdomain.com/whatever.
At one point I had everything except https://olddomain.com redirecting properly. Now I've broken it and I'm just getting the too many redirects error.
I believe both domains have a Let's Encrypt certificate attached to them. The old domain doesn't need to be secured if that makes a difference.
According to https://serverfault.com/a/728957, you can use this snippet to help redirect users to a https://www of the site.
RewriteEngine On
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Hope that helps!

Redirect HTTP to HTTPS for ALL Subdomains

I'm trying to write a rule that means if anyone goes to any address across the whole domain, it will replace http: with https:
My current rule is;
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mydomain.co.uk/members$1 [R,L]
This works for my main domain but I have different subdomains which I need to write to work in the same way. I'd ideally not like to write a rule for each of the subdomains.
If I go to https://example.mydomain.co.uk/members it shows the correct content but if I got http://example.mydomain.co.uk/members it redirects to https://mydomain.co.uk
Thanks in advance for your help!
EDIT: I forgot to mention, this rule is in my /members directory not root.
You could do this:
ServerAlias *.mydomain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$
RewriteRule ^(.*)$ https://%1.mydomain.com/$1 [R=302,L]
try this:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

htaccess redirect www to non-www and http to https

I found different solutions for www to non-www, http to https, or a combination of these two. However, not all of them work as expected. For example this one, it cannot fulfil my third condition below.
Conditions
1. domain.com/* -> https://domain.com/*
2. www.domain.com/* -> https://domain.com/*
3. https://www.domain.com/* -> https://domain.com/*
I combined two of the solutions I found and got the below. It works, but I am not sure if it's correctly written and used. I totally have no knowledge about using .htaccess rules. So I am hoping someone can give a hand. Thank you!
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
I think redirecting www to non-www is better done with a simple DNS CNAME record:
NAME: www.domain.com.
TYPE: CNAME
RECORD: domain.com
That would leave you with only the http to https redirect in Apache, which it looks like you have covered.

Enforce https without www for subdomain

I have phpbb forum which I would like to be viewed only over SSL.
For an example it will be like
If www.subdomain.domain.com - https://subdomain.domain.com
Or
If subdomain.domain.com - https://subdomain.domain.com
I tried several answers which I could find non of them are working properly. Some returning 500 internal server error some aren't enforcing only SSL.
Can anyone suggest me something which I could use in htaccess?
Try
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?sub.domain.com
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://subdomain.com/$1 [L,R]

Redirect non-www to www excluding subdomain

First of all, please don't mark this question as duplicate because I've tried all other answers and no one works for me. I have a subdomain called account.domain.com and this is the code in my .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I want to redirect visits from domain.com to www.domain.com, this works fine. The problem comes when I access the subdomain account.subdomain.com and it redirects to www.account.subdomain.com/account/.php... I don't know why.
I've tested a lot of different codes but I get the same result. My site is hosted in a free account in Hostinger.es.
If you want to target only main domain then use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Resources