From 7039b0ff451ec27eda6788f8ba72fea124d53258 Mon Sep 17 00:00:00 2001
From: clarkewd <clarkewd@users.noreply.github.com>
Date: Thu, 10 Sep 2015 23:15:00 -0400
Subject: [PATCH] add_canonical_port_check

---
 canonical.php | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/canonical.php b/canonical.php
index f75a72e..3068105 100644
--- a/canonical.php
+++ b/canonical.php
@@ -63,6 +63,22 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
 		// build the URL in the address bar
 		$requested_url  = is_ssl() ? 'https://' : 'http://';
 		$requested_url .= $_SERVER['HTTP_HOST'];
+
+		// if a server port is set
+		if(isset($_SERVER['SERVER_PORT']))
+		{
+			// if there is not already a port in the hostname
+			if(strpos($_SERVER['HTTP_HOST'], ':') == false)
+			{
+				// and if the port is a non-standard port
+				if( ! in_array(trim($_SERVER['SERVER_PORT']), array('443','80') ) )
+				{
+					// then explicitly add it to the URL that we are building
+					$requested_url .= ':'.$_SERVER['SERVER_PORT'];
+				}
+			}
+		}
+
 		$requested_url .= $_SERVER['REQUEST_URI'];
 	}
 
-- 
2.3.0

