Fixing ERR_TOO_MANY_REDIRECTS - .htaccess

Im using .htaccess to redirect www. to non www. & http:// to https:// but I'm getting this error in chrome: ERR_TOO_MANY_REDIRECTS and the site isn't working in other browsers. Here is the code I have used in my .htaccess file.
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Any help would be much appreciated.

Looks like you are attempting these 2 things using your rules:
Remove www from domain name
Force https
For this you can use a single rule as this instead of all the code shown:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Make sure to clear your browser cache before testing this change or use a new browser.

Related

redirect http/https subdomain (mail) to https main domain in .httaccess

I know my question is asked many times but unfortunately I could't find my answer. Maybe because I don't put the .htaccess file in the correct place.
What I want is to redirect http://mail.example.com , https://mail.example.com to https://example.com.
I also wanted to redirect https://www.example.com , http://example.com, http://www.example.com to https://example.com. I did this job with adding below code in .htaccees file in public_html in cpanel and it works.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
I used below code from this link and put .htaccess file in mail folder. but when I type http://mail.example.com it redirects to https://mail.example.com and https://mail.example.com also doesn't go to http://example.com. Maybe the problem is with https.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mail\.mysit\.com [NC]
RewriteRule ^(.*) https://example.com/$1 [L,R=301]
Any help is appreciated.
p.s. My site is designed with joomla and there is no redirection in cpanel and joomla settings.
I think everything is correct, and that 'it's only a matter of order:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mail\.example\.com [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
In you main .htaccess file

https://www. to https:// redirect not working

I need to redirect https://www to https://
I've tried every possible snippet of code I could find online to do that over .htaccess
The best one seemed:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
But it's not working for me.
Nothing I do has any effect. If I type https://www.danielleb.com/ - that's where I remain.
How can I make this work?
This is what I am using and it works fine for redirecting https://www. to https://
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Redirect to https://www.example.com

Currently I'm using http://example.com but I want redirect all to https://www.example.com
I read several topics here and tried quite a few solutions to this but nothing worked for me.
My last try was this:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
When I'm using this code I can see this:
enter image description here
But it gives me this message also:
The page isn’t redirecting properly. Firefox has detected that the
server is redirecting the request for this address in a way that will
never complete.
Can you help me with this?
Replace example with your domain and try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Looks to me that you're also trying to force www. You can do both by using this in your .htaccess:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Place this rule to the top of your .htaccess file and make sure you clear your cache before you test it.

.htaccess code not adding automatic www & https before the url

This is my .htaccess code:
# RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
I had added this code to add www & https in the URL forcefully if it is not added. This code worked fine up to some hours ago, but as I tweaked my .htaccess code this code won't work.
In Chrome, Mozilla browsers it's not adding https://www. as a prefix to the URL, it doesn't contain www. or https://www. or http://
I have even reset the cache and data of the Mozilla, chrome browser but the same thing is happening again and again.
Any helps appreciated. Thanks in advance.
To enforce www and https in same rule use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Make sure you clear your browser cache completely before testing this change.
PS: In case you want to do this ONLY for one specific domain then use 3rd RewriteCond as:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(hashstar\.com)$ [NC]

htaccess redirect rules combination

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]

Resources