Skip to content

Commit 6f79a38

Browse files
committed
Added www. redirect
1 parent b61a220 commit 6f79a38

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

index.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
define('HTTPS_REDIRECT', true);
4242

4343

44+
/**
45+
* If true the framework will remove the 'www.' prefix from the domain name by redirecting to the domain without 'www.'.
46+
*/
47+
define('WWW_REDIRECT', true);
48+
49+
4450
// Cookie settings for remembering users language
4551
define('LANGUAGE_COOKIE_NAME', 'L'); // name of cookie to store users last visited language (empty or false to disable)
4652
define('LANGUAGE_COOKIE_EXPIRE_SEC', 5184000); // 60 days
@@ -110,13 +116,20 @@
110116
// INTERAL PROCESSING OF REQUESTS
111117
// ---------------------------------------------------------------------------------------
112118

119+
$IS_HTTPS = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') ||
120+
(!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https' ||
121+
!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) !== 'off');
122+
123+
// check if www. prefix should be removed
124+
if(WWW_REDIRECT && str_starts_with($_SERVER['SERVER_NAME'], 'www.') && empty($_POST)){
125+
header("Location: ".(($IS_HTTPS || HTTPS_REDIRECT) ? 'https' : 'http').
126+
"://".substr($_SERVER['SERVER_NAME'], 4).$_SERVER['REQUEST_URI'].(empty($_SERVER['QUERY_STRING']) ? '' : '?'.$_SERVER['QUERY_STRING']));
127+
exit(0);
128+
}
129+
113130
// check if redirect to https is needed
114-
if(HTTPS_REDIRECT && $_SERVER['REMOTE_ADDR'] !== "127.0.0.1" && $_SERVER['REMOTE_ADDR'] !== "::1" && !(
115-
(isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') ||
116-
(!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https' ||
117-
!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) !== 'off')
118-
)){
119-
header("Location: https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
131+
if(HTTPS_REDIRECT && $_SERVER['REMOTE_ADDR'] !== "127.0.0.1" && $_SERVER['REMOTE_ADDR'] !== "::1" && !$IS_HTTPS && empty($_POST)){
132+
header("Location: https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].(empty($_SERVER['QUERY_STRING']) ? '' : '?'.$_SERVER['QUERY_STRING']));
120133
exit(0);
121134
}
122135

0 commit comments

Comments
 (0)