public
Last active

WordPress SSL

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
## .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>
 
## wp-config.php
define( 'FORCE_SSL_LOGIN', TRUE );
define( 'FORCE_SSL_ADMIN', TRUE );
 
## Plugin
WordPress HTTPS
(http://wordpress.org/extend/plugins/wordpress-https/)
 
## Specific Page/Post for SSL
/**
* Plugin Name: Force SSL for specific pages
* Description:
* Text Domain:
* Domain Path: /languages
* Author: Frank B&uuml;ltge
* Author URI: http://bueltge.de/
* Donate URI: http://bueltge.de/wunschliste/
* 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 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
 
## Without Plugin 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();
}
}
}
 
 
or a smaller version, but not with fallbacks, if the url is wrong
 
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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.