Can't Force HTTPS and Non-WWW via htaccess - apache

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

Related

force redirection from https://example.com to https://www.example.com

I know this is a question that is asked several time. I tried several rules but redirection of https://example.com to https://www.example.com is not working.
My current redirection rule in the Apache vHost of non SSL is pasted below
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE
The above rule works fine for http://example.com and http://www.example.com
I find it. This needs to be added in the ssl vhost file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
My current redirection rule in the apache vHost of non SSL is pasted below
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE
You already have a solution, but as you have found, the above redirect will obviously only apply to HTTP requests when in the "vhost of non ssl". In this case, the HTTPS server variable is always "off" - so the first RewriteCond directive is entirely redundant.
However, you don't need mod_rewrite at all when redirecting from HTTP to HTTPS in the HTTP-virtualhost. A simple mod_alias Redirect will do the job much "better":
Redirect 301 / https://www.example.com/

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

RewriteCond A or (!B and !C)

I can use mod_rewrite in .htaccess file on Apache server. I'd like to have something like this:
RewriteEngine On
RewriteCond %{HTTPS} !on [NC,OR]
RewriteCond %{HTTP_HOST} !^example\.com
RewriteCond %{HTTP_HOST} !^sub\.example\.com
RewriteRule ^/?(.*)$ https://example.com/$1
while meaning "if http or request subdomain other than sub.(e. g. www.), redirect to https and without www.".
https://example.com --> do nothing
http://example.com --> https://example.com
https://www.example.com --> https://example.com
http://www.example.com --> https://example.com
https://sub.example.com --> do nothing
http://sub.example.com --> https://sub.example.com, this will definitely not work with code above
What's the simplest way to do this? Thanks in advance!
You need to break this into 2 different rules:
RewriteEngine On
# remove www and https for https://www.example.com or http://www.example.com
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=302]
# simply add https for rest
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
Make sure to clear your browser cache or use a new browser for testing.
Change 302 to 301 after testing.

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]