2

I am trying to redirect my website to https://www using htaccess 301 permanent redirection rules.

I am using the below code in my htaccess file in Angular 6 build version.

Path: /mysite/dist/myfolder/.htaccess

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.yourwebsite.com/$1 [R=301,L]


<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
</IfModule>

The above code works when I open https://example.com then it redirects to https://www.example.com. This is fine.

But when I try to open example.com or http://example.com then it does not redirect to https://www.example.com.

Gryu
  • 2,102
  • 2
  • 16
  • 29
Mr. Lucifer
  • 119
  • 1
  • 11

2 Answers2

2

Just need to add HTTPS rewrite rule.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.html [L]
</IfModule>
Starycjusz
  • 21
  • 2
0

Try this:

<IfModule mod_rewrite.c>
  Options Indexes FollowSymLinks
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
Tom Balcaen
  • 101
  • 1
  • 5