【问题标题】:Redirect http url to https url using htaccess使用 htaccess 将 http url 重定向到 https url
【发布时间】:2019-12-27 16:04:46
【问题描述】:

我在 htaccess 文件中使用以下代码将 www 重定向到非 www 域,要求如下。

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]


RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]

要求

  1. WWW 到非 WWW

  2. 非 https url 到 https url

  3. 任何其他页面非 http url 到 https url

结果:

  1. 工作正常。

  2. & 3 不起作用,当用户直接在浏览器中输入时,它仍然是 http url。它应该升级到 https url。

代码在 godaddy 共享主机中运行。任何帮助都感激不尽。如果有任何问题,请随时告诉我。

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    我用这个:

    RewriteEngine On
    # the following line is only needed, if HTTP and HTTPS share the same config
    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_FILENAME} !^/.well-known/
    RewriteRule .* https://wiimmfi.de$0 [R=301,L]
    

    我排除了/.well-known/ 来更新我们加密证书。

    <directory> 或.htaccess 中:

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_FILENAME} !^.well-known/
    RewriteRule .* https://wiimmfi.de/$0 [R=301,L]
    

    【讨论】:

    • 这里的.well-known 是什么?它会满足所有 3 个条件吗?
    • 正如我的回答中所说:让我们加密以更新证书的标准目录。
    • 我为这种情况添加了 HTTPS 检查,即 HTTP 和 HTTPS 共享相同的配置。这对于 .htaccess 来说很常见。
    • 如果我们可以在我的 abobe 301 重定向中添加 OR 条件,从所有 http 重定向到 https,它应该可以正常工作,甚至无需修改代码。你能帮我解决一下我的 htaccess 代码吗?
    • 我也在下面使用过,但似乎不能满足我上面的所有要求:RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^www\。 [NC] RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301] RewriteCond %{HTTPS } 在 RewriteCond %{HTTP_HOST} ^www\。 [NC] RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 2023-03-24
    • 1970-01-01
    • 2015-11-02
    相关资源
    最近更新 更多