https for single domain .htaccess - apache

I have a multiple addon domains on my hosting account. I would like to redirect non-https to https for the main domain i.e
andamanexoticholidays.com
my .htaccess approach:
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
but this makes all adon domain to force use ssl by default.
my addon domain are:
radhakrishnaresorthavelock.com
and my sud domains are:
agents.andamanexoticholidays.com
and I want to force use
https://www.andamanexoticholidays.com
a https and www.
any help would be appreciated.

Use a RewriteCondition so that only the main domain can get redirected to https , try :
RewriteCond %{HTTP_HOST} ^(www\.)?andamanexoticholidays.com$
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.andamanexoticholidays.com %{REQUEST_URI} [L,R=301]

Related

htaccess - need to redirect one non https domain to https when multiple domains pointing to the same server

Need to redirect one non https domain to https when multiple domains pointing to the same server.
In detail, all the non www domains need to be redirected to www and only one domain need to be redirected to www as well https.
For example, domain having SSL
http://example.com to https://www.example.com
http://www.example.com to https://www.example.com
https://example.com to https://www.example.com
domain doesn't have SSL
http://example2.com to http://www.example2.com
http://example3.com to http://www.example3.com
I have tried many of the solution provided on stackoverflow like this.
My current htaccess will look like below,
#redirect to https - domain that having SSL
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
#all other non www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Everything is working fine except on one scenario. When I enter domain with www (http://www.example.com) that is having SSL, not redirecting to https.
If I'm actually following your requirements correctly, just add the following first:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

.htaccess - SSL redicect condition for one specific domain not working

I have an apache server with multiple websites hosted:
main-website.com
subdomain1.main-website.com
subdomain2.main-website.com
another-website1.com
another-website2.com
another-website3.com
I need to redirect only https://www.main-website.com to https://www.main-website.com
The subdomains and all the other websites don't need a ssl certificate; therefore I want to exclude them from redirection by specifying that only main-website needs to be redirected.
This is my .htaccess syntax (it seems correct having researched a lot on this topoic)
#NON-WWW to WWW (whis applies to all domains and subdomains)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#redirect HTTP to HTTPS only for main-website.com:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} main-website.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The condition of redirecting main-website.com has been specified, but it does not work! In fact every other domain and subdomain is redirected to HTTPS!!
Do you know where the error could be?
Thank you :)
There must be a typo in your question, you want to redirect from https://www.main-website.com to https://www.main-website.com I will take for granted you wanted to say http://www.main-website.com to https://www.main-website.com
So, your first RewriteCond-RewriteRule set rewrites:
somesite.com ---> https://www.somesite.com
But notice that you also force https in that rediction. Therefore any attempt to reach http://somesite.com will go to https://www.somesite.com
It is probably this rule set that is triggered by your other domains. That first rule should be to rewrite: non www to www, without forcing https at that point.
Then the next rule set will apply only to your main-website and send it to https.
#NON-WWW to WWW (whis applies to all domains and subdomains)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#redirect HTTP to HTTPS only for main-website.com:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} main-website.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have updated the first set of instructions. Still, it does not work :(

Force HTTPS Through HTACCESS With Subdomain Exception

I'm looking to force HTTPS on my entire site except for a subdomain which I am using for a forum. I have an SSL certificate installed on the root domain, but not this forum subdomain. (http://forum.domain.com)
Here is the code for forcing HTTPS:
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
However, this also forces HTTPS on subdomains, which effectively makes the forum inaccessible.
How can I create an exception rule for this subdomain?
To exclude subdomain, you can use a negitive RewriteCond
RewriteCond %{HTTP_HOST} !^sub\.domain\.com$
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess HTTPS main domain and force HTTP on add-on domain

I'm on a shared hosting plan having 1 main domain and 1 add-on domain.
I just installed SSL certificate, however, I encountred the following problem
HTTPS on main domain works fine, but when accessing the addon domain I'm getting redirected to another site with HTTPS. I'm able to access the addon domain without HTTP but would it be possible to force HTTPS to HTTP or remove access to HTTPS for the add-on domain?
This is what I currently have in .htaccess
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Without the add-on domain. Use (instead of your code):
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !addondomain\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Force HTTPS to HTTP for the add-on domain. Add:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} addondomain\.com$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Apache SSL Rewrite with Wildcard Subdomains

I'm attempting to setup an SSL redirect in Apache using RewriteEngine that will do the following:
Redirect traffic to either http://mydomain.com or http://www.mydomain.com to use HTTPS
Redirect traffic to any other subdomain https://*.mydomain.com to use HTTP instead of HTTPS
My reasoning for this is that I'm developing a project that's using a free SSL certificate until launch. This certificate covers the base domain, but none of the wildcard subdomains, and it's a pain to need to bypass the warning every time I visit one of the subdomains.
Edit:
I believe I'm close here, but I still can't get the HTTPS to HTTP redirect to work properly.
RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Figured it out. By moving these rules from the sites-available/default file to an .htaccess inside of the website root, I was able to get this working properly.
RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !=www.mydomain.com
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]