首页 > 解决方案 > Htaccess 重定向到 https 同时有来自 vue-router 的另一个条件

问题描述

我想合并这两个服务器配置:

1) Vue-router docs(正确显示路径)

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

2)我也想重定向用户使用https://,我在网上找到了解决方案:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

如何连接这两者以便我可以在我的.htaccess文件中使用它们?

标签: .htaccessvue-router

解决方案


您可以使用以下规则:

 <IfModule mod_rewrite.c>
  RewriteEngine on
  #enforce https
  RewriteCond %{HTTPS} !=on
  RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
  #rewrite non-existent uris to /index.html
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
 </IfModule>

如果要使重定向永久化(浏览器和搜索引擎缓存),请在 http 到 https 规则中更改R为。R=301


推荐阅读