AngularJS SSL .htaccess woes

佐手、 提交于 2021-02-08 02:12:24

问题


I'm trying to enable the use of SSL on my .htaccess file that has already been set up for html5Mode direct linking but it's always resulting in a redirect loop. The closest question I've found on stackoverflow is this but even from here I can't figure it out (I'm new to htaccess files).

AngularJS html5Mode direct linking htaccess file is

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

and this works perfectly. Explanation here.

Forcing SSL htaccess is

RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

and this also works by itself but when trying to combine them (forcing SSL before routing through index.html) it results in a redirect loop. If I had to guess it would be because it first redirects to using https and then the second redirect routes through index.html using http again. However, I don't know how to test it or fix it.

Any help would be greatly appreciated.

EDIT: As pointed out I should've included what I tried resulting in a loop.

RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html [L]

回答1:


I am not an expert in generating htaccess files, but I found a solution, which is working on my server.

Try this one:

RewriteEngine on

RewriteBase /

RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://%{SERVER_NAME}/index.html [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ https://%{SERVER_NAME}/index.html [L]

It's forcing https and Angular's HTML5 mode is working well too. Don't forget to set up both http (port 80) and https (port 443) urls in your web server config file. Of course this angular code snippet is still really important:

$locationProvider.html5Mode(true);

Good luck, I hope I helped you!



来源:https://stackoverflow.com/questions/30496174/angularjs-ssl-htaccess-woes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!