Directly redirect without www - apache

i have apache server and i have a trouble.
https://i.imgur.com/dMCIvfg.png
Now, when i enter to my site http://example.net, it redirects to https://www then to https
I want
http -> https://
http:/www -> https://
https://www -> https://
It's important that it was directly

First rule get all non https and second rule get all www to https://example.net/
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://example.net%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteRule ^(.*)$ https://example.net/$1 [R=301,L]

Related

htaccess http to https redirection apped twice www in url

Guys i have litle problem with migrating site from HTTP to HTTPS.
I want to force all to www https
http://example.com -> https://www.example.com (wrok)
http://www.example.com -> https.www.example.com (not work)
Above example return url with two www.www
https.www.www.example.com
My code
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
What i do wrong?
You can use this redirect to combine http -> https and www redirects into one rule:
# add www and turn on https in a single redirect rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Your rule on the other hand is attempting to maintain same scheme (http or https) after redirect. Your rule is also working for the case when www is missing. You need to use OR between 2 conditions as I have shown in my answer.

Redirect http url to https url using htaccess

I using below code in the htaccess file to redirect www to non www domain and the requirement as below.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
Requirements:
WWW to Non WWW
http://www.example.com to https://example.com
https://www.example.com to https://example.com
Non https url to https Url
http://example.com to https://example.com
Any other page non http url to https url
http://example.com/blog/7-best-places-to-visit to https://example.com/blog/7-best-places-to-visit
Result:
is working fine as expected.
& 3 is not working and remains http url when user directly enter in browser. It should upgrade to https url.
Code is running in godaddy shared hosting. Any help will be much appreciated. Please feel free to let me know if any questions.
I use this:
RewriteEngine On
# the following line is only needed, if HTTP and HTTPS share the same config
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !^/.well-known/
RewriteRule .* https://wiimmfi.de$0 [R=301,L]
I excluded /.well-known/ to update the let's encrypt certificates.
Inside a <directory> or for .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !^.well-known/
RewriteRule .* https://wiimmfi.de/$0 [R=301,L]

Multiple URL redirections happening from non https www to https www

Have a small wordpress blog and was following this guide. One of the step was to change htaccess file for redirects for non-www and www to https www. According to https://varvy.com/tools/redirects/ there should only be one redirect on each type.
But for my no www no http to https www there is 2 redirect happening.
http://myportal.com
301 redirect
https://myportal.com/
https://myportal.com/
301 redirect
https://www.myportal.com/
Many of the tools are taking this as a negative and saying too many redirects. How can I make the 2 step into 1 step? So the result comes to
http://myportal.com
301 redirect
https://www.myportal.com/
Currently have the following code.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can use the following rule to force https and www in a single URL redirection.
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*)$ https://www.%1%{REQUEST_URI} [L,R=301]
Note : Remove the non-www to to www redirect rule If you already have that rule in your htaccess otherwise that might conflict with this one.
Make sure to clear your browser cache before testing this new rule.

Redirect to https always with www optimization using htaccess

I need to redirect everything to my domain to use https://www and below is the .htaccess I am currently using:
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It is working but it makes 2 redirects to the browser one if missing the www and the second if missing the secure which is slow of course and may be bad.
What I want or my question is can this be reduced to single redirect rule to make it add both the www and the https in one rule.
This online test tool shows 2 redirects https://varvy.com/tools/ :
Final status code: 200
2 Redirect(s)
http://domain.com
301 redirect
https://domain.com/
https://domain.com/
301 redirect
https://www.domain.com/
Any good optimizations to this code.
You can use:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
2 rules but never more than one redirection
You can use just 1 single rule
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
non-www to www redirect is not needed here because you want to redirect both versions to https://www .
Clear your browser's cache before testing this.

Apache redirect rule optimize

I'm trying to optimize my redirect
RewriteEngine On
RewriteCond %{HTTP_HOST} !^boycottplus\.org$
RewriteRule ^ https://boycottplus.org%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Part 1 is to redirect users that visit the .com site to the .org site
Part 2 is to redirect users that visit unsecure (http) to https
Is there a way to both redirect .com user to the secure .org site, and redirect http users on the .org site to the https site? Both while keeping the {request_uri} intact?
Use both conditions in one rule while applying OR logic instead of default AND:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^boycottplus\.org$ [OR]
RewriteCond %{HTTPS} =off
RewriteRule ^ https://boycottplus.org%{REQUEST_URI} [L,R=301]