Redirect http://example.com to https://www.example.com on Apache - apache

I want to redirect http://example.com to https://www.example.com on Apache
Using: https://varvy.com/tools/redirects/ with my domain https://www.a-c-d.net
I have two redirects in no www.
In my .htaccess I have:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.a-c-d.net%{REQUEST_URI} [R=301,L]
In https://varvy.com/tools/redirects/ I want to have 1 redirect in www, no www, no www(https) and 0 redirects in www(https).
Thanks
Ben

Related

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

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]

How to edit htaccess to redirect all trafic to secure www domain

How to achieve the following:
redirect all http:// domain .com to https:// www.domain .com
redirect all http:// www.domain .com to https:// www.domain .com
redirect all https:// domain .com to https:// www.domain .com
So basically, all my traffic be it non-www or www will be redirected to SSL www domain.
Edit:
Additionally, I have 2 subdomain that works on http, so I want to achieve the above and also keep the 2 subdomain free from the above redirect rule.
You can use the following code in Root/.htaccess :
RewriteEngine on
#Http to https
#Exclude subdomains
RewriteCond %{HTTP_HOST} !^(sub1|sub2)
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NC,L,R]
#add www on ssl
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NC,L,R]
This will redirect :
http://example.com
or
http://www.example.com
to ssl
https://www.example.com
And the second rule will add www to non www requests on ssl, redirect :
https://example.com
to
https://www.example.com
First, you can edit your httpd.conf file, and add this to the :80 servers
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com$1 [L,R=301]
</IfModule>
Anyway , this on .htaccess will do the same
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) https://www.domain.com/$1 [L,R,NE]
Also, you can add in your vhost file also something like
Redirect permanent / https://www.domain.com/
And in your vhost_ssl:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com$1 [L,R=301]
</IfModule>

Redirect http to https and also no-www to www

I want to redirect the following:
http://example.com to https://www.example.com
http://www.example.com to https://www.example.com
https://example.com to https://www.example.com
'www.example.com' to https://www.example.com
You can add these mod_rewrite rules into an .htaccess file at the root of your site:
RewriteEngine On
RewriteBase /
# www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Note that you will need the mod_rewrite module enabled. Also, you may need to adjust the .htaccess permissions/overrides in your Apache config.

HTACCESS add WWW with HTTPS redirect

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