redirecting https://domain.com to https://www.domain.com - apache

I've recently moved my site to HTTPS and im trying to force all http traffic to https and also to the www. version
i.e forcing
https://domain.com
to
https://www.domain.com
I have this in my htaccess and all http to https works, apart from the redirect from https://domain.com to https://www.domain.com
What am i doing wrong?
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

You need an OR condition here:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
with the above rule https://example.com was not redirecting to https://www.example.com.
For that to happen I have added the following rule too in the apache ssl vhost block
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.exmple.com%{REQUEST_URI} [R=301,L,NE]

Related

.htaccess how to handle dynamic sub-domain redirect to https and no sub-domain to www

I have my htaccess file gitignored and while upgrading to Laravel 5.2 I deleted the old one on the server. I spent hours piecing it together and now I'm struggling to recreate it.
I need anything like
http://domain.com
or
https://domain.com
to goto
https://www.domain.com
and all other wildcard sub-domains such as:
http://myname.domain.com
to goto
https://myname.domain.com
So far I have this:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
To redirect subdomains and main domain to HTTPS, you can use:
RewriteEngine on
#--Redirect subdomain to https--#
RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.domain.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NC,R,L]
#--Redirect maindomain to https --#
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [NC,R,L]
#--Redirect non-www to www on SSL--#
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTPS} on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NC,L,R]
Change R to R=301 when you are sure the redirect is working fine.

Combining Rules in .htaccess file?

I'm wondering how to connect these two rewrite rules in one .htaccess file. Currently, I force https connections to my website with this rule:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:HTTPS} !=on
RewriteCond %{HTTP_HOST} !=SERVER_NAME
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Now I want to redirect from my www-subdomain to my real domain. There is a high rated answere here:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But I fail to put these two together.
You can use:
RewriteEngine On
# if with www, redirect to https domain without www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# if http, redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
If you test first for www and redirect to https, no need to test for http://www.
Or like #PanamaJack said, you just can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://domain.com%{REQUEST_URI} [NE,L,R=301]
But you need to change the domain name.

Redirect https to non http and Http to Https in directory with htaccess

I want to make my homepage to redirect to non https if it https
if https://example.com will redirect to http://example.com
and i want to make directory with ssl (just directory using SSL)
if http://example.com/directory wil redirect to https://example.com/directory
I'm use this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
but it just show loop
RewriteEngine On
RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_URI} !^/dir
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} ^off$
RewriteCond %{REQUEST_URI} ^/dir
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Rules .htaccess to redirect from http://miodominio.it to https://www.miodominio.com

I have to domains:
miodominio.it
miodominio.com
The ".com" is under an SSL certificate. Now I want to redirect every call from the ".it" to the ".com" but the SSL certificate is valid only for the ".com".
I use those rules in my .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^miodominio\.it$ [OR]
RewriteCond %{HTTP_HOST} ^www\.miodominio\.it$
RewriteRule ^/?$ "https\:\/\/www\.miodominio\.com" [R=301,L]
Anyway when I go to this link: https://miodominio.it/ it's not redirected to https://www.miodominio.com and it gives me a privacy error.
What's the correct rule I should use to redirect it correctly?
Thanks in advance.
From https to http (like in your comment):
This rule redirects every request to example.com.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteCond %{HTTPS} on
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R=301]
or just for your example:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.it
RewriteCond %{HTTPS} on
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R=301]
From http to https (like in your title):
RewriteCond %{HTTP_HOST} ^www.it.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^exp.it$ [NC]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.exp.com/$1 [R=301,L]
....

Redirect https and http www to https non www

I want to rewrite the following
http://example.com
http://www.example.com
https://www.example.com
to
https://example.com
tried using
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
It doesn't seems to be working
I'd say the easiest thing to do is just write two rules:
# First make sure HTTPS is being used
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Then make sure www isn't being used
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can use:
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]