Skip to content

Commit 9cd5a5f

Browse files
committed
Fix HTTPS detection heuristic and unconditional URL rewriting (v1.4)
1 parent 3ae1d1f commit 9cd5a5f

3 files changed

Lines changed: 33 additions & 37 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [1.4] - 2026-06-19
6+
### Fixed
7+
- HTTPS detection no longer uses a hostname-based heuristic (checking for a dot in `HTTP_HOST`). It now relies exclusively on a genuine `X-Forwarded-Proto: https` header, preventing false HTTPS detection on direct, non-proxied connections.
8+
- `home_url()` and `site_url()` filters are now gated on the detected HTTPS state, so URLs are only rewritten to `https://` when HTTPS has actually been detected. Previously the rewrite was unconditional, which forced HTTPS URLs even on plain-HTTP paths.
9+
510
## [1.3] - 2025-09-11
611
### Added
712
- Initial public release

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ A lightweight WordPress plugin that enforces HTTPS detection and correct REST AP
44

55
## Features
66

7-
- Forces `$_SERVER['HTTPS'] = 'on'` when proxy headers or domain heuristics indicate HTTPS
8-
- Rewrites `home_url()` and `site_url()` to always return `https://`
7+
- Forces `$_SERVER['HTTPS'] = 'on'` when a genuine `X-Forwarded-Proto: https` header is present
8+
- Rewrites `home_url()` and `site_url()` to `https://` when HTTPS has been detected (no longer unconditional)
99
- Ensures Rank Math SEO plugin uses the correct REST API base
1010
- Compatible with Dockerized WordPress stacks and reverse proxy setups
1111

proxy-aware-https-fix.php

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,37 @@
33
/**
44
* Plugin Name: Proxy-Aware HTTPS Fix
55
* Description: Enforces HTTPS detection and correct REST API resolution behind reverse proxies.
6-
* Version: 1.3
6+
* Version: 1.4
77
* Author: BitCryptic
88
*/
99

10-
if ( defined( 'WP_CLI' ) && WP_CLI ) {
11-
return;
12-
}
10+
if (defined('WP_CLI') && WP_CLI) return;
1311

14-
add_action(
15-
'plugins_loaded',
16-
function () {
17-
$host = $_SERVER['HTTP_HOST'] ?? '';
18-
$proto = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '';
12+
add_action('plugins_loaded', function() {
13+
$proto = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '';
1914

20-
if (
21-
( ! isset( $_SERVER['HTTPS'] ) || $_SERVER['HTTPS'] !== 'on' ) &&
22-
( $proto === 'https' || strpos( $host, '.' ) !== false )
23-
) {
24-
$_SERVER['HTTPS'] = 'on';
25-
}
26-
}
27-
);
15+
if (
16+
(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') &&
17+
$proto === 'https'
18+
) {
19+
$_SERVER['HTTPS'] = 'on';
20+
}
21+
});
2822

29-
add_filter(
30-
'home_url',
31-
function ( $url ) {
32-
return str_starts_with( $url, 'http:' ) ? 'https:' . substr( $url, 5 ) : $url;
33-
}
34-
);
23+
add_filter('home_url', function($url) {
24+
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
25+
return str_starts_with($url, 'http:') ? 'https:' . substr($url, 5) : $url;
26+
}
27+
return $url;
28+
});
3529

36-
add_filter(
37-
'site_url',
38-
function ( $url ) {
39-
return str_starts_with( $url, 'http:' ) ? 'https:' . substr( $url, 5 ) : $url;
40-
}
41-
);
30+
add_filter('site_url', function($url) {
31+
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
32+
return str_starts_with($url, 'http:') ? 'https:' . substr($url, 5) : $url;
33+
}
34+
return $url;
35+
});
4236

43-
add_filter(
44-
'rank_math/rest_api_url',
45-
function () {
46-
return get_site_url( null, '/wp-json/' );
47-
}
48-
);
37+
add_filter('rank_math/rest_api_url', function() {
38+
return get_site_url(null, '/wp-json/');
39+
});

0 commit comments

Comments
 (0)