重定向到网站的 https 版本?

Redirect to https version of the site?

如何保留 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= [QSA,L]
</IfModule>

您的 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= [QSA,L]
</IfModule>

我删除了 RewriteRule substitution 字符串上的 ./ 前缀。这不是必需的 (并且在这种情况下并没有真正意义)并且将被 OS.

解决

此外,考虑删除 <IfModule> 包装器 - 此处不需要它,除非您的代码被移植到多个服务器,其中 mod_rewrite 可能未启用并且您的站点预计在没有它的情况下工作(尽管这似乎不太可能)。参见 my answer to the following question on the Webmasters Stack for more information regarding this: Is Checking For mod_write Really Necessary?