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

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]

Related

My website http://www.alfarestents.com/ to https://www.alfarestents.com/ redirection doesn't work

http://www. version of my doesn't get redirected to https version (all other variants are properly redirected)
Website url:
http://www.alfarestents.com/
https://www.alfarestents.com/
Htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS} ^on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteCond %{HTTP_HOST} ^alfarestents.com [NC]
RewriteRule ^(.*)$ https://www.alfarestents.com/$1 [L,R=301]
Let's start this from scratch:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,R=301,END]
It probably is a good idea to start with a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as expected.
You should implement such rules in the real http server's host configuration. You can also use a distributed configuration file, but that comes with a performance penalty. Also you need to enable the consideration of such files first.

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]

Fixing ERR_TOO_MANY_REDIRECTS

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.

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