ayZagen
ayZagen

Reputation: 527

HTTP redirect to HTTPS while keeping angular routes (.htaccess)

All I want to do is preventing the site for going http rather than https. Here is my .htaccess configuration. Only www.mywebsite.com and mywebsite.com doesn't go https. Angular routes are ok too. If i write mywebsite.com/signup it goes https as well. What should i do to be able to redirect all scenarios to https ?

SCENARIOS:

www.website.com -> not https

website.com -> not https

website.com/signin -> https

www.website.com/signin -> https

RewriteEngine On

  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
  RewriteRule ^ - [L]


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

Upvotes: 4

Views: 6237

Answers (1)

Nekudotayim
Nekudotayim

Reputation: 181

The first 2 rewrite conditions will ignore existing files and directories, but the root directory (normally) always exists. Try to remove the first block.

This will be sufficient:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

For angular routes to work you need another block like this

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html

That way all non SSL traffic will be redirected. The second part will rewrite everything to your angular index file, except existing directories and files

Upvotes: 7

Related Questions