首页 文章

http到https重定向使用htaccess在角度6中不起作用

提问于
浏览
2

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

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

路径:/mysite/dist/myfolder/.htaccess代码:RewriteCond上的RewriteEngine% off [OR] RewriteCond%!^ 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 .

2 回答

  • 2

    只需要添加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>
    
  • 0

    试试这个:

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

相关问题