I'm trying to run my entire site through https and force www.
I've seen a number of solutions that provide forcing either www or https, and even a few combined, but I can't seem to get any to work. I usually find myself in a redirect loop.
The closest I've got is the following, but it's not close enough yet:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
I need https://www.example.com/
http://example.com SUCCESS
https://example.com SUCCESS
http://www.example.com FAIL
https://www.example.com SUCCESS, although there's no actual redirection.
Thanks
Update
The following code successfully performs the redirection I require:
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Forcing https and www is done by a combination of other provided answers.
This seems to work:
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Try this condition instead, it should force the site into ssl for your domain
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Try adding an [OR] flag to your conditions. You really want either no www or no https:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
Related
I got www ssl certificate. I've to redirect my site https://www.domain-name.com/folder/home.php whenever user typing like http://domain-name.com or https://domain-name.com or domain-name.com
I tried many ways. It's working when I type domain-name.com but it shows privacy error and not redirect while using https://domain-name.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]
Please change
RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]
To
RewriteRule ^(.*)$ https://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]
Try this one:
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
I have to redirect my domain from http to https. In my htaccess I have already.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
This snippet redirect everything without "www" to "www".
When I change this to
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
The result is:
http://www.example.com/folder/page.php
becomes
Location => https://www.example.com/folder/page.php
Fine!
https://example.com/folder/page.php
becomes
https://www.example.com/folder/page.php
Fine!
but:
http://example.com/folder/page.php
becomes
Location => https://example.com/folder/page.php
but it has to be
Location => https://www.example.com/folder/page.php
How is it possible to fix this?
I know all of this redirects:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
but I need only one redirection instead of two 301.
You can use the following rule
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{RERUEST_URI} [NE,L,R=301]
Clear your browser cache before testing this.
I have found a solution here:
https://webmasters.stackexchange.com/questions/84757/htaccess-redirect-non-www-to-www-with-ssl-https
The second answer by #w3dk is working.
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Your site needs to be accessible by both www and non-www over SSL for the .htaccess redirect to trigger.
With letsencrypt its no problem to have this 2 certs.
My domain www.abc.com is now redirecting to https://www.abc.co.uk
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]
These code works exactly what I want. After that I created a subdomain. www.x.abc.co.uk
Now I want to redirect that to http://x.abc.co.uk (without https)
I used this
RewriteCond %{HTTP_HOST} ^x\.abc\.co\.uk [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But browser says it has redirect loop.
How can do that?
If these are two different htaccess files then give this a try.
RewriteEngine On
#redirect www.x.abc.co.uk without www to http
RewriteCond %{HTTP_HOST} ^www\.x\.abc\.co\.uk [NC]
RewriteRule ^(.*)$ http://x.abc.co.uk%{REQUEST_URI} [L,R=301]
I modified your original rule so you don't have two rewrite rules and still direct to www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.abc.co.uk%{REQUEST_URI} [L,R=301]
What I am currently using is:
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
What I actually want is:
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [AND]
RewriteCond %{REQUEST_URI} !\/folder/next_folder/(custom_id)
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
As you can see I am trying to add an extra conditions before rewriting to https.
For all pages I used https but now firefox has an update and gives problems with Mixed content (when loading iframe that uses http:// instead of https://):
https://blog.mozilla.org/security/2013/05/16/mixed-content-blocking-in-firefox-aurora/
That's why I need to exclude a few pages from using https:// notice I remove the www. too.
I tried different things but still getting errors or it just does not work like it supposed to do. I appreciate the help.
Move the rewrite condition to the part where http: is ON not where https: is already ON.
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/folder/next_folder/custom_id
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
I thought I could get this accomplished using the following, but it's looping...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteCond %{HTTPS_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
What do I need to do to make sure all instances of the domain go to https://example.com without looping?
Your first line checks if the host is what you are redirecting to. You don't need to check HTTPS_HOST, because in https, the host is still the same (it's from the request header, Host:).
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]