htaccess rewrite rules - domain/subdomain https/www - apache

I have my main domain and a subdomain. Subdomain is located in a subdirectory on the root directory of main domain and this cannot be changed. Main domain has an SSL while subdomain doesn't.
Now I am trying to force https and www for the main domain and force no https and no www for the subdomain. So at the end the main domain disregarding how it will try to be accessed should redirect at https://www.example.com and same time subdomain should be http://sub.example.com
All other possible combinations of accessing them should redirect to the above 2. So far, I am successfull to do this for the most possibilities but I am failing to do so for the case when I access the subdomain with https://sub.example.com. It won't redirect to the http version, rather it will try to load the page through https, browser throws the security warning about the SSL and if I proceed, it load the contents of the main domain site through the subdomain and the insecure warning on the browser.
Following is the htaccess rules I am trying, on the htaccess on the root directory, that affects both main site and subdomain.
How to improve them and achieve the desired outcome?
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} sub\.example\.com [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} sub\. [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !sub\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} www\.sub\. [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]

To redirect only the main domain to https, you can use the following rule :
RewriteEngine on
#https to http (subdomain)
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^sub.example.com$
RewriteRule ^ http://sub.example.com%{REQUEST_URI} [NE,L,R]
#main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

Related

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/

Redirect all subdomain to main domain except one selected

If I want to redirect all subdomains to a main domain, except one subdomain that I need redirect to a subfolder. Is there any way?
Redirect selected subdomain to subfolder
Redirect all subdomains to main domain
This is my Apache configuration:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mySubdomain\.domain\.com$ [NC]
RewriteRule ^ http://domain.com/mySubdomain [L,R]
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule (.*) http://domain.com/$1 [L,R=301,QSA]
The first rule is correct to serve a particular subdomain from a directory. For the second rule just add 1 AND condition that if the current request is not for domain.com AND not for mySubdomain.domain.com then redirect request to domain.com as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mySubdomain\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/mySubdomain/$1 [R]
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mySubdomain\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301]

Redirecting Subdomain to WWW using .htaccess

I'm trying to redirect a specific subdomain to www. I already have rules in place that will redirect non-www to www.domain.com. Here is what I currently have, and it works fine:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
My problem is that some requests are coming in for ns2.domain.com, and I would like those redirected to www.domain.com as well. Any help?
Try adding an extra [^.]+\. in your second condition:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

.htaccess to redirect http to https for sensitive areas

I am just finally updating my site from Joomla 1.5 and Virtuemart 1.1.9 to Joomla 2.5 and Virtuemart 2. I have tried enabling the SSL for sensitive areas in Virtuemart but it is not working and I would like to use .htaccess to do the redirect instead.
I have tried this code and it would work if my site was located in the root of the website but it is in a subfolder i.e. www.uniqbuy.com/electronics
How can I get this code to not strip electronics from the URL when redirecting to https?
# Force SSL on checkout login account and admin pages
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} checkout|login|my-account|administrator|webshop
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ https://%2/$1 [R=301,L,QSA]
# Remove SSL on other pages
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !checkout|login|my-account|administrator|webshop
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%2/$1 [R=301,L,QSA]
# Force www for non https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,QSA]
Any help would be great.
Thanks
Your second rule appears to be using wrong regex. Try this code:
# Force SSL on checkout login account and admin pages
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (checkout|login|my-account|administrator|webshop)
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$ [NC]
RewriteRule ^(.*)$ https://%2/$1 [R=301,L]
# Remove SSL on other pages
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(checkout|login|my-account|administrator|webshop)
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%2/$1 [R=301,L]
# Force www for non https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,QSA]

Ignore No Subdomain In htaccess RewriteCond

I've got htaccess set up that redirects all subdomains except www to https. However I'd also like it to ignore http://domain.com - currently it redirects to https://domain.com which is no good for what I'm doing.
Any idea what the rewrite condition is for that?
I've tried:
RewriteCond %{HTTP_HOST} !^domain.com [NC]
RewriteCond %{HTTP_HOST} !^http://domain.com [NC]
But neither of those work.
Here's what I'm using at the minute.
# Redirect subdomains to https
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^domain.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I think your problem is that you need to escape the dot in domain.com, so domain\.com.
# Redirect subdomains to https
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^domain\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You used a 301 (permanent) and not a 302, so you may have your browser not even trying to send requests to the http domain until you close. You should use 302 while testing and put the 301 only wen it's ok.
Try this:
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]