我正在尝试使用htaccess 301永久重定向规则将我的网站重定向到https://www。
我在Angular 6构建版本的htaccess文件中使用以下代码。
路径:/ mysite/dist/myfolder/.htaccess代码:RewriteCond上的RewriteEngine%{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。
只需添加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>
试试这个:
<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>