Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]
like image 614
ayZagen Avatar asked Oct 27 '25 12:10

ayZagen


1 Answers

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

like image 147
Nekudotayim Avatar answered Oct 30 '25 02:10

Nekudotayim