1

Angular provides a default .htaccess file in its documentation:

RewriteEngine On

# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

However, this does not force https://.

Whatever I try (using answers or snippets from similar questions about https:// forcing), it always breaks the routing, and results in either 404 or the Apache is functioning normally notice.


How do I update this snippet to force https://, while keeping the routing intact?

1 Answer 1

8

Try this

RewriteEngine On

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

# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
Sign up to request clarification or add additional context in comments.

2 Comments

That seems to work, dankjewel! Initially this resulted in 404 too, but I think the problem was that I tried it on my dev environment, without https configured. On production, it works great!
seems to be valid but got the too many redirects errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.