ayZagen ayZagen -4 years ago 144
AngularJS Question

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 ?


www.website.com -> not https

website.com -> not https

website.com/signin -> https

www.website.com/signin -> https

RewriteEngine On

RewriteRule ^ - [L]

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download