我有一个Angular 5应用程序,我需要将所有流量重定向到Https,而不是这种情况。我只有访问.htaccess才能做到这一点。
起初,我有这样的配置:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>可以很好地避免404并让Angular处理路由,但当我尝试添加https时,它破坏了一切。
我尝试了多种方法,首先我尝试添加:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]但是我有太多的重定向,无法加载页面
然后我尝试将我的conf替换为:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . https://%{SERVER_NAME}/index.html [L]
</IfModule>所有的URI都被处理了,但是在Angular中它们总是重定向到"/“,所以我不能使用Url来导航(例如,我有一个只能通过URL访问的/admin部分)
我不习惯apache,我通常使用nginx,有人知道如何解决我的问题吗?
谢谢
发布于 2018-03-20 16:07:58
如果有人有同样的问题,我设法用这个.htaccess使它工作:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]
RewriteBase /dist
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>我猜我第一次尝试的错误是https测试的条件。
发布于 2018-07-11 02:23:20
谢谢mel-mouk在我的例子中,我的dist文件直接在public_html中,所以我不需要重写基础,我尝试了你的解决方案,但没有它,但它不起作用,最终我认为我有一个好的想法为什么它失败(通过试验和错误/实验),它是在每一行之间,我的服务器显然需要一个空行,例如:
<IfModule mod_rewrite.c>
RewriteEngine On而不是:
<IfModule mod_rewrite.c>
RewriteEngine On这非常奇怪,但我理解为什么会发生这种情况。无论如何,我想我应该让其他人知道,如果你完全不能工作,我会让其他人知道。
https://stackoverflow.com/questions/49359948
复制相似问题