Redirect all requests to non-www https - .htaccess

I've searched for a solution of my problem on SO but none of the answers worked for me.
This is what i want to achieve:
Redirect all
http://example.com -> https://example.com
http://www.example.com -> https://example.com
https://www.example.com -> https://example.com
Basically i want that whatever form of the domain is accessed to be redirected to non-WWW https (https://example.com)

You can use in your .htaccess:
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

Related

How to redirect http://example.com to https://www.example.com

My situation is the following visiting my website...
Typing in browser http://example.com it redirects to http://www.example.com (with www. but insecure), and this is my problem.
Instead, if I type in the browser http://www.example.com it redirect correctly to https://www.example.com (secure connection).
I need to redirect both the URLs http://example.com and http://www.example.com always to https://www.example.com.
How to reach this result?
Thanks a lot for your help.
My .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*) https://www.example.com/$1 [R=301,L]
</IfModule>
Edit: the solution is to force https redirection:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]

redirect everything to https://www for main domain except for for subdomains

I want these redirects
http://example.com => https://www.example.com
http://www.example.com => https://www.example.com
https://example.com => https://www.example.com
https://example.com/asa.php => https://www.example.com/asa.php
http://subdomain.example.com => https://subdomain.example.com
https://www.subdomain.example.com => https://subdomain.example.com
What I have tried is
#redirect http or https to https//www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But these rules handle all domains of the site, not just the main site.
The idea is to make all the incoming http to https.
And all main site non-www to www.
And for subdomain from www to non-www if any.
All in all, these are three separate requests.
change http to https, this should already work as is
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
redirect main site example.com to www.example.com. This can be done by being more specific
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R,L]
redirect non main sites from www.subdomain.example.com to subdomain.example.com. To strip the leading www, you can capture the part after www., and use the captured subdomain in the rewrite rule
RewriteCond %{HTTP_HOST} ^www\.(.+\.example\.com)$
RewriteRule ^ https://%1%{REQUEST_URI} [R,L]
Finally, put 1. at the end to avoid two redirects for e.g. http://example.com or http://www.subdomain.example.com
Unrelated, but never test with R=301!

rewrite https and www

now I have a website https.
http://www.example.com -> https://www.example.com = OK !
But, I have a problem with www
://example.com -> s://example.com = not good
I would like to have this redirection below :
://example.com -> s://www.example.com
How with RewriteCond and RewriteRule ? Help me plz !
Thanks !
To enforce https on all requests and redirect to www.example.com
# force https and www
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R,L]
# if already https without www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R,L]

Redirecting a site to https and non www .htaccess

I cant seem to find the answer I want to redirect to https and the non www version of a site
so http://www.example.com redirects to https://example.com
You can do this in a single rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://domain.com%{REQUEST_URI} [L,R=301,NE]

How to redirect all website requests to HTTPS with no www

I would like to make it so that all requests to my website are redirected to HTTPS with no www. Right now I apparently have both a www and non www version of my site which is supposed to be bad for seo (duplicate content). I also just added an SSL certificate and need to redirect all traffic to the https version of my site. Below is an example of what I am looking to accomplish:
example.com -> https://example.com
www.example.com -> https://example.com
http://www.example.com -> https://example.com
https://www.example.com -> https://example.com
How would I accomplish this within my htaccess file?
try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>
These rules need to be at the top of the htaccess file in your document root
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

Resources