1

Trying to redirect all http and none-www traffic to https (with or without www). Using .htaccess and apache VirtualHost files:

VirtualHost file

<VirtualHost *:80>
    ServerName adi-md.co.il
    ServerAlias www.adi-md.co.il

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP_HOST} ^adi-md.co.il
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE]
#RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
#RewriteRule (.*) https://www.%1%{REQUEST_URI} [L,R=301]
#RewriteCond %{HTTP_HOST} ^www.adi-md.co.il

option 1

RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^adi-md.co.il

option 2

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://www.%1%{REQUEST_URI} [L,R=301]

When typing just adi-md.co.il you get the the server's default page.

How do I fix it so all traffic is both redirected to https as well as the none-www requests??

Note: I have looked at these 3 but they do not supply an answer (2 out 3 deal with www to none-www)

apache redirect http to https and www to non www
http to https apache redirection
Redirect from http to https without www using .htaccess

Community
  • 1
  • 1
Jadeye
  • 3,551
  • 4
  • 47
  • 63

2 Answers2

1

Two simple rules for redirect http to https without www:

RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Oleh Vasylyev
  • 684
  • 4
  • 21
0

Try using this:

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} =on
RewriteRule ^ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE]

This basically first checks if if you either have www enabled and HTTPs turned on. If either are, it will switch it back to HTTP and remove www.

Make sure you clear your cache before testing this.

Joe
  • 4,877
  • 5
  • 30
  • 51