【问题标题】:How to redirect to https index.html with .htacess如何使用 .htaccess 重定向到 https index.html
【发布时间】:2021-11-18 15:20:26
【问题描述】:

我有一个 vue-laravel 水疗中心。为了让 vue 应用程序在历史模式下正常工作,我需要确保它重定向到 index.html。我正在遵循 vue cli 文档中的说明,并在 .htaccess 文件中使用这段代码

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

同时,我还需要确保所有 http 请求都被重定向到 https,我的 hostgator 托管为我提供了这段代码。

RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteRule ^/?$ "https\:\/\/subdomain\.domain\.com\/" [R=301,L]

问题是这两段代码似乎不能一起工作。但是,当另一个被注释掉时,它们中的每一个都可以工作。

【问题讨论】:

    标签: vue.js .htaccess single-page-application vue-cli


    【解决方案1】:

    顺序很重要。规范(HTTP 到 HTTPS)重定向需要重写为index.html

    RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
    RewriteRule ^/?$ "https\:\/\/subdomain\.domain\.com\/" [R=301,L]
    

    然而,这个写成的规则会重定向到自身,并会导致重定向循环! (这不是 HTTP 到 HTTPS 重定向的一部分。)

    您不需要 &lt;IfModule&gt; 包装器,也不需要重复 RewriteEngine 指令。

    换句话说:

    RewriteEngine On
    
    # Redirect HTTP to HTTPS
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
    
    # Front-Controller
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
    

    你实际上并没有使用RewriteBase 指令,所以我删除了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2014-10-01
      • 2020-09-03
      • 2011-08-02
      • 1970-01-01
      • 2012-12-11
      相关资源
      最近更新 更多