HTACCESS add WWW with HTTPS redirect - apache

Currently my htaccess code is
#add www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
#send all traffic to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
This works when following urls are entered
1. https://example.com -> https://www.example.com
2. http://example.com -> https://www.example.com
3. http://www.example.com -> https://www.example.com
4. https://example.com -> https://www.example.com
5. https://example.com/somepage -> https://www.example.com/somepage
6. http://www.example.com/somepage -> https://www.example.com/somepage
But it doesn't work when both https and www is not present while trying to access some page, instead it redirect to strange url
7. http://example.com/somepage -> https://www.example.com/https://example.com/somepage

Replace your current code by this one
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Note: maybe you'll have to clear your browser's cache to see it working for http://example.com/somepage

Related

Directly redirect without www

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]

Redirect www and http to non-www with https using .htaccess

I've got the following written into my .htaccess file which is placed in the directory where my index.html is.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URL} [L,R=301]
I want that
www.example.com
http://example.com
and http://www.example.com
are redirected to
https://example.com
Do I have to exchange REQUEST_URI with example.com or am I doing something else wrong ?
You must put an OR in between your rewrite conditions
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
Note: You have to test the urls as:
http://www.example.com
http://example.com
https://www.example.com
They will all be redirected to
https://example.com

Can't Force HTTPS and Non-WWW via htaccess

I am trying to force any regular http request to redirect to https, and also force non www in the URLs, so that every request in the end will be https://example.com
http://example.com becomes https://example.com
http://www.example.com becomes https://example.com
https://www.example.com becomes https://example.com
http://example.com becomes https://example.com
I have the following code in my htaccess file.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
But I am getting an error that states it has too many redirects. It seems to work fine on this test site with the correct output:
htaaccess tester preview
Any ideas or a better approach to this?
The following code should help you:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
It will redirect everything to https://example.com

Redirect to https and www with one 301 redirect

all
I want to redirect all requests to land on https://www URL, e.g.
case1: http://www.example.com --301--> https://www.example.com
case2: https://example.com --301--> https://www.example.com
case3: http://example.com --301--> https://www.example.com
Here I have my code in .htaccess file
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]
It works fine in case 1 and 2. However when I have a http and non-www request (case3), it will require two 301 redirects to land on https://www
http://example.com
--301-->
https://example.com
--301-->
https://www.example.com
How can we force https and www with always just 1 301 redirect.
I am not very familiar with Apache and rewrite module. Help please!
To redirect to https://www in a single request, you can use :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]

Merging a HTTPS redirect with WWW redirect

I currently have this in my .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
This ensures all http://www.example.com is redirected to http://example.com
I would like to implement an SSL certificate and I found this is the rewrite rule I must use for http to redirect to https.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
How could I merge the HTTPS condition with my current condition so that
http://www.example.com redirects to https://example.com
http://example.com redirects to https://example.com
If it is not possible to merge this, can I just have those two code snippets one after another?
Thanks in advance.
Yes, you can use them one after another. I'd also save your users a redirect by having your first set of rules send them straight to HTTPS.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]