.htaccess - redirect all to non-www - .htaccess

How to redirect all pages to non-www https?
http://example.com/* -> https://example.com/*
http://www.example.com/* -> https://example.com/*
https://www.example.com/* -> https://example.com/*
Thanks

Use:
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

Redirect All Subdomains to Root

I need all subdomains to redirect back to the root.
Ex). test.website.ca -> https://www.website.ca
fake1.website.ca -> https://www.website.ca
www.ggg.website.ca/ -> https://www.website.ca
etc.
Here is the code that I tried:
RewriteCond %{HTTP_HOST} ^(.+)\.website\.ca$ [NC]
RewriteRule (.*) https://www.website.ca/$1 [L,R=301,QSA]
You can use:
RewriteCond %{HTTP_HOST} !^www\.website\.ca$ [NC]
RewriteRule ^ https://www.website.ca%{REQUEST_URI} [NE,L,R=301]
Without the uri, use same RewriteCond and:
RewriteRule ^ https://www.website.ca/ [L,R=301]

htaccess redirect domain to https, subdomain to https and non-www to www

I’m looking to do this:
Force the https for my main domain.
http to https://www.
http://www to https://www.
But not for subdomains
http://subdomain.domain.com to https://subdomain.domain.com
Can some one help me i can't find this
You can use that in your root .htaccess:
RewriteEngine on
# redirect to https www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
# redirect to http subdomain
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^((?!www).+\.domain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
[EDIT]
RewriteEngine on
# redirect no-www to www only main domain, not with subdomain
RewriteCond %{HTTP_HOST} ^(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
# redirect http to https all domain
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Modifying current rewrite rules to force SSL

I want all of the pages on my website to force an SSL connection. Currently I have this in place...
#RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^(www)\.domain\.com$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The above rules were inspired by the internet and I do not fully understand them. I tried changing the last line to RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] (note the https part).
When I test the redirect using http://www.domain.com/path-to-here I am redirected to https://www.domain.com/.
In short, the ultimate goal is to force www and https while preserving all original paths.
Ex:
http://domain.com -> https://www.domain.com
http://domain.com/this-is-my-path-1/ -> https://www.domain.com/this-is-my-path-1/
https://domain.com -> https://www.domain.com
https://domain.com/this-is-my-path-1/ -> https://www.domain.com/this-is-my-path-1/
http://www.domain.com -> https://www.domain.com
http://www.domain.com/this-is-my-path-1/ -> https://www.domain.com/this-is-my-path-1/
You can use:
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [NE,R=301,L]

How to force https and www. in .htaccess?

comI tried following code in .htaccess to force https and www. Because I allways want a URL like https://www.domain.com
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^.*domain\.com$ [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L]
If i type in these cases into the browser it works fine:
www.domain.com
domain.com
http://www.domain.com
http://domain.com
But if i type following line - no forwarding happens:
https://domain.com
How can i fix this?
You need these 2 rules for all the redirections:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]

htaccess rewrite http://example.com to http://www.example.com but https://www.example.com to https://example.com

Using .htaccess rewrite, I'd like to have:
http://example.com redirect to: http://www.example.com
https://www.example.com redirect to: https://example.com
Which is the best way to do this?
I've started with:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You need 2 rules:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

Resources