問題描述

我想在某些頁面 (帶表單的頁面) 上強制安全連線,但是我不希望整個網站使用 ssl(減慢它)

有沒有辦法配置特定頁面來要求 ssl?

最佳解決方案

使用 admin-ssl 外掛。對於 wp 之外的東西,在 apache 中使用 rewriite 規則

次佳解決方案

新的工作流程,因為不支援管理 SSL 外掛。

  • 使用外掛 WP https

  • 檢視設定

  • 如果要使用 wp-admin 的 SSL,請將其新增到 wp-config.php 中:

    define( 'FORCE_SSL_ADMIN', TRUE );
    
  • 如果您還要登入頁面的 SSL,請將其新增到 wp-config.php

    define( 'FORCE_SSL_LOGIN', TRUE );
    
  • 將以下行新增到.htaccess; 刪除 WP 的預設值

    <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteCond %{SERVER_PORT} !^443$
       RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    </IfModule>
    
  • 如果您在 front-end 中將特定頁面/帖子設定為 SSL,則使用以下外掛或在 post /page 的編輯器中設定選項; 只有當你有這種外掛 WP https 的可能性。另請參見 Gist 4081291 作為示例外掛

    /**
     * Plugin Name: Force SSL for specific pages
     * Description:
     * Author:      Frank Bültge
     * Author URI:  http://bueltge.de/
     * Version:     1.0.0
     */
    
    ! defined( 'ABSPATH' ) and exit;
    
    if ( ! function_exists( 'fb_force_ssl' ) ) {
    
        add_filter( 'force_ssl' , 'fb_force_ssl', 1, 3 );
        function fb_force_ssl( $force_ssl, $id = 0, $utrl = '' ) {
            // A list of posts/page that should be SSL
            $ssl_posts = array( 22, 312 );
    
            if ( in_array( $id, $ssl_posts ) )
                $force_ssl = TRUE;
    
            return $force_ssl;
        }
    
    } // end if func exists
    
  • 無外掛 WordPress HTTPS

    add_action( 'template_redirect', 'fb_ssl_template_redirect', 1 );
    function fb_ssl_template_redirect() {
    
            if ( is_page( 123 ) && ! is_ssl() ) {
    
                if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
                    wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']), 301 );
                    exit();
                } else {
                    wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
                    exit();
                }
            } else if ( !is_page( 123 ) && 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();
                }
            }
    }
    

或者較小的版本,但是如果網址錯誤,則不能使用回退

add_filter( 'pre_post_link', 'fb_set_ssl_url', 10, 3 );
function fb_set_ssl_url( $permalink, $post, $leavename ) {

    if ( 123 == $post->ID )
        return preg_replace( '|^http://|', 'https://', $permalink );

    return $permalink;
}

第三種解決方案

對於 WordPress 3.0 及以上版本,admin-ssl 外掛不起作用。為了使 SSL 工作,您需要採取兩個步驟:

  1. 啟用 wp-config.php 檔案 (see here) 中的 「管理層 SSL」 選項。

  2. 在網站上安裝 WPSSL 外掛。 (更新為 WordPress 3.0+)

  3. 在要透過 SSL 執行的頁面上,新增名為”force_ssl” 的元標記,並將其值設定為”true” 。

你應該全部設定好。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。