Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong baseurl when running behind proxy #89

Open
tmakinen opened this issue Aug 10, 2023 · 3 comments
Open

Wrong baseurl when running behind proxy #89

tmakinen opened this issue Aug 10, 2023 · 3 comments

Comments

@tmakinen
Copy link

I have setup where php4dvd is running behing proxy. Looks like baseurl is set according to last hop so all assets (js, css, etc) are loaded from wrong URL. If i'm reading code correctly problem is here:

https://github.com/jreklund/php4dvd/blob/master/common.inc.php#L77

This should first check if header HTTP_X_FORWARDED_HOST is set and use that and only fallback to $_SERVER["HTTP_HOST"] if header is not set.

@jreklund
Copy link
Owner

That value should be statically set by the user instead of automatically trying to figure out the right one. Or it's better to just use relative URI and stop comparing it to the domain. 🤔

Will figure something out.

@jreklund
Copy link
Owner

Please check if this modified code works on your setup.

$baseurl = "/";
$Website->assign("baseurl", $baseurl);

// Current URL
$currentUrl = $baseurl . ltrim($_SERVER["REQUEST_URI"], "/");
$Website->assign("currentUrl", $currentUrl);

// Webroot
$basepath = $settings["url"]["base"];
if(strlen($basepath) > 0 && !preg_match("/\/$/", $basepath)) {
	$basepath .= "/";
}
$webroot = $baseurl . ltrim($basepath, "/");
$Website->assign("webroot", $webroot);

I also removed "base url" and "force the use of HTTPS" parts from this file as well.

@tmakinen
Copy link
Author

tmakinen commented Aug 14, 2023

Yes this fixes errors. Just in case here is diff that i used:

--- common.inc.php.orig	2023-08-14 16:49:52.064423051 +0000
+++ common.inc.php	2023-08-14 16:50:33.635497024 +0000
@@ -53,32 +53,16 @@
 	}
 }
 
-// Base url
-$protocol = "http";
-if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) {
-	$protocol = "https";
-}
-if(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
-	$protocol = "https";
-}
-
-// Force the use of HTTPS
-if(
-	isset($settings["url"]["HTTPS"]) && $settings["url"]["HTTPS"] && $protocol !== 'https'
-) {
-	header('Location: https://'. $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"], true, 301);
-	exit();
-}
 
 // Load smarty template parser
 require_once($loc . "lib/Website.class.php");
 $Website = new Website($settings);
 
-$baseurl = $protocol . "://" . $_SERVER["HTTP_HOST"];
+$baseurl = "/";
 $Website->assign("baseurl", $baseurl);
 
 // Current URL
-$currentUrl = $baseurl . $_SERVER["REQUEST_URI"];
+$currentUrl = $baseurl . ltrim($_SERVER["REQUEST_URI"], "/");
 $Website->assign("currentUrl", $currentUrl);
 
 // Webroot
@@ -86,7 +70,7 @@
 if(strlen($basepath) > 0 && !preg_match("/\/$/", $basepath)) {
 	$basepath .= "/";
 }
-$webroot = $baseurl . "/" . $basepath;
+$webroot = $baseurl . ltrim($basepath, "/");
 $Website->assign("webroot", $webroot);
 
 // Include util functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants