迹忆客 EN >

当前位置:主页 > 学无止境 > 网络 >

如何将网站从HTTP重定向到HTTPS

作者:迹忆客 最近更新:2022/12/26 浏览次数:

HTTPS 是一种用于通过计算机网络进行安全通信的协议,在 Internet 上广泛使用。越来越多的网站所有者正在从 HTTP 迁移到 HTTPS,主要是由于以下 5 个原因:

  1. 谷歌宣布使用 HTTPS 的网站将在谷歌搜索中获得轻微的排名优先级。
  2. 由于浏览器支持,我们可以使用新的HTTP/2协议实现更快的性能,该协议需要 HTTPS。
  3. HTTPS 更安全,访问者的数据完全加密。
  4. HTTPS 可以通过在访问者的 Web 浏览器地址栏中启用绿色锁标识来建立信任。
  5. 如果有人从 HTTPS 访问网站并转到 HTTP 网站,则引荐数据将在 Google Analytics 中丢失。它通常最终与“直接流量”混为一谈。如果有人从一个 HTTPS 网站转到另一个 HTTPS 网站,推荐数据仍然会被传递。因此,通过从HTTP迁移到 HTTPS,我们实际上可以获得更准确的推荐数据。

如何将 HTTP 重定向到 HTTPS

说了这么多,那么如何将HTTP重定向到HTTPS呢,具体包括以下三种方法

  • 在 Nginx 中将 HTTP 重定向到 HTTPS
  • 在 Apache 中将 HTTP 重定向到 HTTPS
  • 使用真正简单的 SSL 插件将 HTTP 重定向到 HTTPS

注意:我们的示例都包含 301 重定向指令,这是在 SEO 方面实现它的正确方法。使用不同类型的重定向可能会对网站的排名产生不同的影响。

在 Nginx 中将 HTTP 重定向到 HTTPS

根据 W3Techs 的数据,Nginx是增长最快的 Web 服务器,截至 2017 年拥有30% 以上的市场份额。平均而言,每分钟前 1000 万个网站中就有一个开始使用 Nginx。

nginx图示

如果你的 Web 服务器正在运行 Nginx,则可以通过将以下代码添加到 Nginx 配置文件中,轻松地将所有 HTTP 流量重定向到 HTTPS。这是重定向在 Nginx 上运行的网站的推荐方法。

server {
    listen 80;
    server_name jiyik.com www.jiyik.com;
    return 301 https://jiyik.com$request_uri;
}

迹忆客就是使用的此种方式实现的HTTP重定向到HTTPS

在 Apache 中将 HTTP 重定向到 HTTPS

apache图示

如果你的 Web 服务器正在运行Apache,则可以通过将以下代码添加到.htaccess 文件中,轻松地将所有 HTTP 流量重定向到 HTTPS 。这是重定向在 Apache 上运行的网站的推荐方法。

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

使用真正简单的 SSL 插件将 HTTP 重定向到 HTTPS

如果网站是使用WordPress开发的,则我们必须从 HTTP 重定向到 HTTPS 的第三个选项是使用免费的 WordPress真正简单的 SSL插件

我们不建议将此方法作为永久解决方案,因为 3rd 方插件总是会引入另一层问题和兼容性问题。此外,对于 HTTPS 迁移,我们应该更新数据库中的 HTTP URL,而不是依赖插件。但这可以是一个很好的临时解决方案。

really-simple-ssl-plugin

该插件有超过 20 万的活跃安装,由开发人员 Rogier Lankhorst 维护。我们可以从 WordPress 存储库下载真正简单的SSL,也可以在WordPress 控制面板中的“添加新”插件下搜索它。以下是插件功能的列表:

  • 所有传入的 HTTPS 请求都从 HTTP 重定向到 HTTPS。如果可能,使用.htaccess,否则使用 JavaScript。
  • WordPress 站点 URL 和主页 URL 更改为 HTTPS。
  • 通过将所有 HTTP://URL 替换为 HTTPS:// 来修复不安全内容,但指向其他外部域的链接除外。一切都是动态完成的。除了 WordPress 站点 URL 和主页 URL 之外,没有进行其他数据库更改。

使用此插件实际上没有任何步骤,只需安装并单击“继续,激活 SSL”就可以了。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

HTTP POST request in Node.js

发布时间:2025/04/17 浏览次数:131 分类:Node.js

In this article, we will learn how to use Node.js to make a post request using a third-party package. HTTP Post Request in Node.js The HTTP POST method creates or adds resources on the server. The key difference between POST and PUT request

Redirection in PHP

发布时间:2025/04/13 浏览次数:158 分类:PHP

header() We will demonstrate another way to redirect a page in PHP using the function by sending an HTTP header to the browser . This method uses the built-in header() function in PHP, which takes Location as a parameter, and its value is t

Linux data stream bidirectional redirection command tee

发布时间:2025/04/08 浏览次数:177 分类:OPERATING SYSTEM

The tee command is a member of the Linux pipe command family. Its function is to redirect data to a file. We know that data redirection can be done directly using . Yes, can redirect data streams. But it cannot redirect data to standard out

Apache2.4 installation and precautions under Windows7

发布时间:2025/04/08 浏览次数:125 分类:OPERATING SYSTEM

To install apache2.4 in Windows 7, we first need to download the apache2.4 installation program. Here we download the software from the apache official website http://httpd.apache.org/download.cgi . First, let's see how to download apache2.

How to Append Text to a File Using Bash

发布时间:2025/04/05 浏览次数:83 分类:OPERATING SYSTEM

We can use the redirection ( ) operator and tee the command to append text to a file. We have to make sure we have enough permissions to add text to the file. If we don't have enough permissions, we may get a permission denied error. Use th

Redirecting Stderr and Stdout to a file in Bash

发布时间:2025/03/23 浏览次数:187 分类:OPERATING SYSTEM

In this article, we will discuss standard output and standard error in Linux. We will see how to redirect standard output and standard error in Bash. Let us start by understanding the terms standard output and standard error in Linux. Stand

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便