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]
Related
I need to automatically add www if thereĀ“s no subdomain in the url
https://example.com => https://www.example.com
http://test.example.com => http://test.example.com (no change)
it also will be nice to make it work for both http and https.
this is what I have so far
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
You can use this :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.[^.]+)$
RewriteRule ^ https://www.%2%{REQUEST_URI} [NE,L,R]
I want to redirect from
http -> https
and
without www -> with www
via htacess file.
I need both in combination in one htacess file.
How are the rewrite rules for this combination? Thank you.
All you need to do is use this:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE]
It checks if www and HTTPs are enabled. If not, it forces them both to on.
Make sure you clear your cache before testing this.
EDIT: Try this.
RewriteEngine on
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]
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]
I want to redirect https://example.com to https://www.example.com
And if I go to https://example.com/aboutus,
it should also redirect to https://www.example.com/aboutus.
Currently, I have this in .htaccess but it make ssl certificate disappear.
RewriteCond %{HTTPS} off
RewriteCond %{DOCUMENT_ROOT}/../.force_ssl -f
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Just try this rule at top of your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Make sure to clear your browser cache.
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]