問題描述
我想在某些頁面 (帶表單的頁面) 上強制安全連接,但是我不希望整個網站使用 ssl(減慢它)
有沒有辦法配置特定頁面來要求 ssl?
最佳解決方案
使用 admin-ssl 插件。對於 wp 之外的東西,在 apache 中使用 rewriite 規則
次佳解決方案
新的工作流程,因為不支持管理 SSL 插件。
-
使用插件 WP https
-
查看設置
-
如果要使用
wp-admin的 SSL,請將其添加到wp-config.php中:define( 'FORCE_SSL_ADMIN', TRUE ); -
如果您還要登錄頁面的 SSL,請將其添加到
wp-config.phpdefine( '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 工作,您需要採取兩個步驟:
-
啓用 wp-config.php 文件 (see here) 中的 「管理層 SSL」 選項。
-
在網站上安裝 WPSSL 插件。 (更新為 WordPress 3.0+)
-
在要通過 SSL 運行的頁面上,添加名為”force_ssl” 的元標記,並將其值設置為”true” 。
你應該全部設定好。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。