首页 > 解决方案 > http 到 https 重定向在使用 htaccess 的角度 6 中不起作用

问题描述

我正在尝试使用 htaccess 301 永久重定向规则将我的网站重定向到https://www 。

我在 Angular 6 构建版本的 htaccess 文件中使用以下代码。

小路:/mysite/dist/myfolder/.htaccess

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.yourwebsite.com/$1 [R=301,L]


<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
</IfModule>

上面的代码在我打开https://example.com时有效,然后它重定向到https://www.example.com。这可以。

但是当我尝试打开 example.com 或http://example.com时,它不会重定向到https://www.example.com

标签: angular.htaccess

解决方案


只需添加 HTTPS 重写规则。

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.html [L]
</IfModule>

推荐阅读