.htaccess - Htaccess 重定向到 https,同时有来自 vue-router 的另一个条件

标签 .htaccess 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 文件中使用它们?

最佳答案

您可以使用以下规则:

 <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

关于.htaccess - Htaccess 重定向到 https,同时有来自 vue-router 的另一个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55549337/

相关文章:

button - 在 vuejs 的按钮中包含一个路由器链接标签

javascript - 微前端/Web 组件/Vue 路由器错误 : Uncaught TypeError: Cannot redefine property: $router

javascript - Vue路由器: HTML5 Canvas image links won't route to the right URL

javascript - Vue Router next 回调总是在 beforeEach 中调用

php - 子文件夹中的附加 .htaccess | mod_rewrite 用于查询参数

javascript - 将我的 JS MIME 类型设置为 "text/javascript"

php - 在 Laravel 应用程序中通过 .htaccess 利用浏览器缓存无法正常工作

javascript - Tawk.to(免费实时聊天脚本)产生 400 错误请求错误

apache - htaccess : add path parameter (country code) to URL & serve index. html

javascript - 如何在 vue-router 中设置 afterEach 处理程序