【问题标题】:Redirect to https version of the site?重定向到网站的 https 版本?
【发布时间】:2021-02-17 07:49:32
【问题描述】:

如何保留index.php 文件的逻辑,但从http 重定向到https 版本的网站?

我的 .htaccess

AddDefaultCharset utf8

ErrorDocument 404 /404.php

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f

    RewriteRule ^(.*)$ ./index.php?route=$1 [QSA,L]
</IfModule>

【问题讨论】:

    标签: .htaccess redirect mod-rewrite https http-redirect


    【解决方案1】:

    您的 HTTP 到 HTTPS 重定向只需要先进行,现有的内部重写之前。

    例如:

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        # HTTP to HTTPS redirect
        RewriteCond %{HTTPS} off
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
        # Internal rewrite
        RewriteCond %{SCRIPT_FILENAME} !-d
        RewriteCond %{SCRIPT_FILENAME} !-f
        RewriteRule (.*) index.php?route=$1 [QSA,L]
    </IfModule>
    

    我删除了RewriteRule 替换 字符串上的./ 前缀。这不是必需的 (在这种情况下并没有真正的意义)并且会被操作系统解决。

    另外,请考虑删除 &lt;IfModule&gt; 包装器 - 此处不需要它,除非您的代码被移植到可能未启用 mod_rewrite 的多个服务器,并且您的网站在没有它的情况下也可以正常工作(尽管这似乎不太可能)。有关此问题的更多信息,请参阅 my answer 到网站管理员堆栈上的以下问题:Is Checking For mod_write Really Necessary?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 2015-05-01
      • 1970-01-01
      • 2013-07-03
      相关资源
      最近更新 更多