简体   繁体   English

WordPress 将所有 HTTPS 重定向到 HTTP

[英]WordPress redirect all HTTPS to HTTP

We have a WordPress site, and used to have an SSL certificate.我们有一个 WordPress 站点,并且曾经拥有 SSL 证书。 The site used to be all HTTPS, and now we don't need the SSL anymore so we let it expire.该站点以前都是 HTTPS,现在我们不再需要 SSL,因此我们让它过期。

We've already changed the Site Address and WordPress Address in the admin panel to be http://example.com .我们已经将管理面板中的站点地址WordPress 地址更改为http://example.com

We have several links out in the wild that link back to us with https:// and if the user accesses the site with https:// the site breaks or the user gets a warning message in their browser.我们在野外有几个链接,它们通过https://链接回我们,如果用户使用https://访问站点,则站点会中断或用户在浏览器中收到警告消息。

Bottom line, we need to redirect all https:// traffic to http:// .最重要的是,我们需要将所有https://流量重定向到http://

I tried couple of plugins (no luck):我尝试了几个插件(运气不好):

and even changed the .htaccess file (still no luck)甚至更改了.htaccess文件(仍然没有运气)

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Not sure what else I need to do.不知道我还需要做什么。

The problem here lies with the fact that before Apache or WordPress come in to play, the browser needs to establish a connection with the server over HTTPS by connecting, performing an SSL handshake, exchanging (and verifying) certificates, and only after all that is done, will the browser issue the HTTP request that tells the server what resources it is looking for.这里的问题在于,在 Apache 或 WordPress 发挥作用之前,浏览器需要通过连接、执行 SSL 握手、交换(和验证)证书来通过 HTTPS 与服务器建立连接,并且只有在所有这些之后完成后,浏览器会发出 HTTP 请求,告诉服务器它正在寻找什么资源。

Because of that, no .htaccess or WordPress plugin is going to be able to redirect the user without them establishing a secure session.因此,没有 .htaccess 或 WordPress 插件将能够在没有建立安全会话的情况下重定向用户。

Of course if you install a self-signed certificate, the user is going to be presented with a warning before any of this happens.当然,如果您安装了自签名证书,则在发生任何这种情况之前,用户都会收到警告。 If you by chance (which doesn't seem to be the cast) had been sending Strict Transport Security headers over https, then previous visitors' browsers may not even allow them to connect over HTTP.如果您偶然(似乎不是演员)一直通过 https 发送严格的传输安全标头,那么以前访问者的浏览器甚至可能不允许他们通过 HTTP 连接。

If you want to redirect HTTPS traffic to HTTP, unfortunately you are going to have to acquire a valid certificate and redirect using .htaccess or some PHP code as you are.如果您想将 HTTPS 流量重定向到 HTTP,不幸的是,您将必须获得有效证书并使用 .htaccess 或一些 PHP 代码进行重定向。

If you're looking for certificates that are trusted by a majority of browsers without paying, you can get a free certificate from Let's Encrypt .如果您正在寻找大多数浏览器信任且无需付费的证书,您可以从Let's Encrypt获取免费证书。

Bottom line, if you want to seamlessly redirect HTTPS traffic to HTTP without any warning messages, you need to install another SSL certificate from a trusted CA.最重要的是,如果您想在没有任何警告消息的情况下将 HTTPS 流量无缝重定向到 HTTP,您需要从受信任的 CA 安装另一个 SSL 证书。

Here is an alternative solution you can use if you don't want to edit .htaccess :如果您不想编辑.htaccess则可以使用以下替代解决方案:

add_action( 'template_redirect', 'nonhttps_template_redirect', 1 );

function nonhttps_template_redirect() {

    if ( is_ssl() && !is_admin() ) {

        if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {

            wp_redirect( preg_replace( '|^https://|', 'http://', $_SERVER['REQUEST_URI'] ), 301 );

            exit();

        } else {

            wp_redirect( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );

            exit();

        }

    }

}

You can place this at the bottom of your theme functions.php你可以把它放在你的主题functions.php的底部

Expanding on @HigherCoding 's answer and @MrWhite 's comment, to get a PHP function to do this on a site where the https port exists but the ssl certification is invalid, expired or non-existent:扩展@HigherCoding 的回答和@MrWhite 的评论,以获得一个 PHP 函数在https端口存在但 ssl 证书无效、过期或不存在的站点上执行此操作:

function shapeSpace_check_https() { 
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) { 
    return true; 
}
    return false;
}


function bhww_ssl_template_redirect() {
if ( shapeSpace_check_https() ) {
    if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {    
        wp_redirect( preg_replace( '|^(https://)|', 'http://', $_SERVER['REQUEST_URI'] ), 301 );
        exit(); 
    } else {
        wp_redirect( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
        exit();     
    }
}   
}

add_action( 'template_redirect', 'bhww_ssl_template_redirect');

This worked on my site in functions.php and was a combination of these two sources: Source 1 and Source 2 .这在我的网站上的functions.php并且是这两个来源的组合: Source 1Source 2

As @drew010 pointed out - this will still not prevent a scary prompt for users who type in https as part of your URL.正如@drew010 所指出的那样 - 这仍然不会阻止将https作为 URL 一部分输入的用户的可怕提示。 But it will redirect them to http if they happen to click through the scary prompt, which is unlikely.但是,如果他们碰巧点击了可怕的提示,它会将他们重定向到http ,这不太可能。 It seems that getting an SSL certificate is likely the best option for this reason, for general security and for increased Google ranking now & in the future.出于这个原因,获得 SSL 证书似乎是最好的选择,无论是为了一般的安全性,还是为了提高现在和未来的 Google 排名。

The htaccess way is simple especially if you don't have access to admin area in WordPress. htaccess 方法很简单,特别是如果您无权访问 WordPress 中的管理区域。

Just paste this below RewriteCond %{HTTPS} on只需将其粘贴到 RewriteCond %{HTTPS} 下面

RewriteRule (.*) HTTP://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

in addition to your lines please try to add these lines in wp-config.php除了你的行,请尝试在 wp-config.php 中添加这些行

define( 'WP_CACHE', false ); 
define('WP_HOME','http://adwordslead.com');
define('WP_SITEURL','http://adwordslead.com');
define('FORCE_SSL_ADMIN', false);
define( 'FORCE_SSL_LOGIN', false );

hopefully, your error will resolve希望你的错误会解决

This disables https completely这将完全禁用 https


    <IfModule mod_rewrite.c> 
        RewriteEngine On
        RewriteCond %{HTTPS} off
    </IfModule>

This leaves The HTTPS enabled and then uses server VARIABLES to redirect all pages to HTTP:这将启用 HTTPS,然后使用服务器变量将所有页面重定向到 HTTP:


    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTPS} on
        RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}
    </IfModule>

Hope this helps you....希望这对你有帮助......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM