diff --git a/extension/Classes/Core/Helper/OnString.php b/extension/Classes/Core/Helper/OnString.php
index de4bbe91955864972fef1ebd29a4c35dd7ddd211..3b8cf6f3628a4c3d0f18fae97ed1b003fa696e85 100644
--- a/extension/Classes/Core/Helper/OnString.php
+++ b/extension/Classes/Core/Helper/OnString.php
@@ -669,5 +669,43 @@ class OnString {
         }
         return $value;
     }
+
+
+    /*
+     * Check the given $url for ending with a index.php or a query or an anker.
+     * If one of them found: remove it.
+     * Return cleaned $url
+     */
+    public static function urlStripFile($url) {
+
+        if ($url == '') {
+            return '';
+        }
+
+        // Take care all %dd are replaced by real characters
+        $url = urldecode($url);
+
+        // Explode to examine last part
+        $arr = explode('/', $url);
+
+        $last = count($arr) - 1;
+
+        if ($arr[$last] == '') {
+            unset($arr[$last]);
+            $last--;
+        }
+
+        if ($last < 1) {
+            return $url;
+        }
+
+        // Last component starts with 'index.php' or contains a '?' or '#' - that's likely not to be a part of baserurl
+        if (0 == strcmp('index.php', substr($arr[$last], 0, 9)) ||
+            strpos($arr[$last], '?') !== false || strpos($arr[$last], '#') !== false) {
+            unset($arr[$last]);
+        }
+
+        return implode('/', $arr);
+    }
 }
 
diff --git a/extension/Classes/Core/Store/Config.php b/extension/Classes/Core/Store/Config.php
index c2e5a2b83a3664cfd65d5382ef96268bc708688b..ad272eecfd5fa2e88631830c4276a64564ef948b 100644
--- a/extension/Classes/Core/Store/Config.php
+++ b/extension/Classes/Core/Store/Config.php
@@ -100,15 +100,11 @@ class Config {
         }
 
         // Set default from baseUrl if not given
-        if($config[SYSTEM_BASE_URL] === ''){
-            $fullUrl = parse_url($_SERVER["SCRIPT_URI"]);
-            $fullUrl['sections'] = explode('/', $fullUrl['path']);
-            $baseUrl = $fullUrl['scheme'].'://'.$fullUrl['host'];
-            for($i = 1; $i < sizeof($fullUrl['sections']) -1; $i++){
-                $baseUrl .= '/'.$fullUrl['sections'][$i];
-            }
-            T3Handler::updateT3QfqConfig(SYSTEM_BASE_URL, $baseUrl); //Legacy behaviour.
-            $config[SYSTEM_BASE_URL] = $baseUrl;
+        if ($config[SYSTEM_BASE_URL] === '') {
+
+            $config[SYSTEM_BASE_URL] = OnString::urlStripFile($_SERVER["SCRIPT_URI"]);
+
+            T3Handler::updateT3QfqConfig(SYSTEM_BASE_URL, $config[SYSTEM_BASE_URL]); // Legacy behaviour.
         }
 
         $config = self::renameConfigElements($config);
@@ -137,7 +133,8 @@ class Config {
      * @return array
      * @throws \UserReportException
      */
-    private static function getCustomVariable(array $config) {
+    private
+    static function getCustomVariable(array $config) {
 
         for ($i = 1; $i <= 30; $i++) {
             if (isset($config['custom' . $i])) {
@@ -166,7 +163,8 @@ class Config {
      * @throws \UserFormException
      * @throws \CodeException
      */
-    private static function writeConfig(array $config) {
+    private
+    static function writeConfig(array $config) {
         $absoluteConf = Path::absoluteConf();
         HelperFile::createPathRecursive($absoluteConf);
         HelperFile::file_put_contents(Path::join($absoluteConf, CONFIG_QFQ_JSON), json_encode($config, JSON_PRETTY_PRINT));
@@ -178,7 +176,8 @@ class Config {
      * @throws \CodeException
      * @throws \UserFormException
      */
-    public static function migrateConfigPhpToJson(): void {
+    public
+    static function migrateConfigPhpToJson(): void {
         // read old config.qfq.php
         $absoluteOldConfigFilePath = Path::absoluteApp(Path::APP_TO_TYPO3_CONF, CONFIG_QFQ_PHP);
         if (!is_writeable($absoluteOldConfigFilePath)) {
@@ -211,7 +210,8 @@ class Config {
      * @throws \UserFormException
      * @throws \UserReportException
      */
-    private static function readTypo3QfqConfig(): array {
+    private
+    static function readTypo3QfqConfig(): array {
         $configT3qfq = array();
         if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][EXT_KEY])) {
             // Typo3 version >=9
@@ -249,7 +249,8 @@ class Config {
      * @param array $db
      * @return mixed
      */
-    private static function getDbName(array $db) {
+    private
+    static function getDbName(array $db) {
 
         // T3 7.x: $GLOBALS['TYPO3_CONF_VARS']['DB']['database'],  T3 8.x: $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['dbname']
         return isset($db['database']) ? $db['database'] : $db['Connections']['Default']['dbname'];
@@ -261,7 +262,8 @@ class Config {
      * @param array $config
      * @throws \UserFormException
      */
-    private static function checkDeprecated(array $config) {
+    private
+    static function checkDeprecated(array $config) {
 
         foreach ([SYSTEM_VAR_ADD_BY_SQL] as $key) {
 
@@ -284,7 +286,8 @@ class Config {
      * @throws \UserFormException
      * @throws \UserReportException
      */
-    public static function checkForAttack(array $config) {
+    public
+    static function checkForAttack(array $config) {
         $attack = false;
         $key = '';
         $reason = 'Problem: ';
@@ -346,7 +349,8 @@ class Config {
      * @throws \UserFormException
      * @throws \UserReportException
      */
-    public static function attackDetectedExitNow(array $config = array(), $reason = '') {
+    public
+    static function attackDetectedExitNow(array $config = array(), $reason = '') {
 
         if (count($config) == 0) {
             $config = self::getConfigArray();
@@ -398,7 +402,8 @@ class Config {
      *
      * @return array
      */
-    public static function setDefaults(array $config) {
+    public
+    static function setDefaults(array $config) {
 
         $default = [
 
@@ -514,7 +519,8 @@ class Config {
      *
      * @return array
      */
-    private static function renameConfigElements(array $config) {
+    private
+    static function renameConfigElements(array $config) {
 
         // oldname > newname
         $setting = [
@@ -556,7 +562,8 @@ class Config {
      * @param array $config
      * @return array
      */
-    private static function adjustConfig(array $config) {
+    private
+    static function adjustConfig(array $config) {
 
         $config[SYSTEM_SHOW_DEBUG_INFO] = self::adjustConfigDebugInfoAuto($config[SYSTEM_SHOW_DEBUG_INFO], T3Info::beUserLoggedIn());
         if ($config[SYSTEM_REPORT_MIN_PHP_VERSION] == SYSTEM_REPORT_MIN_PHP_VERSION_AUTO && T3Info::beUserLoggedIn()) {
@@ -584,7 +591,8 @@ class Config {
      * @param $flag
      * @return string
      */
-    public static function adjustConfigDebugInfoAuto($value, $flag) {
+    public
+    static function adjustConfigDebugInfoAuto($value, $flag) {
 
         // Check if SHOW_DEBUG_INFO contains 'auto'. Replace with appropriate.
         if (Support::findInSet(SYSTEM_SHOW_DEBUG_INFO_AUTO, $value) && $flag) {
@@ -600,7 +608,8 @@ class Config {
      * @param array $config
      * @return array
      */
-    private static function setAutoConfigValue(array $config) {
+    private
+    static function setAutoConfigValue(array $config) {
 
         $config[SYSTEM_DB_NAME_DATA] = $config['DB_' . $config[SYSTEM_DB_INDEX_DATA] . '_NAME'] ?? '';
         $config[SYSTEM_DB_NAME_QFQ] = $config['DB_' . $config[SYSTEM_DB_INDEX_QFQ] . '_NAME'] ?? '';
@@ -615,7 +624,8 @@ class Config {
      *
      * @throws \UserFormException
      */
-    private static function checkMandatoryParameter(array $config) {
+    private
+    static function checkMandatoryParameter(array $config) {
 
         // Check mandatory config vars.
         $names = array_merge([SYSTEM_SQL_LOG_MODE],
@@ -633,7 +643,8 @@ class Config {
      * @param $index
      * @return array
      */
-    private static function dbCredentialName($index) {
+    private
+    static function dbCredentialName($index) {
         $names = array();
         $names[] = 'DB_' . $index . '_USER';
         $names[] = 'DB_' . $index . '_SERVER';
diff --git a/extension/Tests/Unit/Core/Helper/OnStringTest.php b/extension/Tests/Unit/Core/Helper/OnStringTest.php
index efb36fbb4074c78ff7e0e21a3d0563c761fc9d1c..64d3847cb524a357b4b3acc22a9758b7be16d89f 100644
--- a/extension/Tests/Unit/Core/Helper/OnStringTest.php
+++ b/extension/Tests/Unit/Core/Helper/OnStringTest.php
@@ -295,4 +295,25 @@ class OnStringTest extends TestCase {
         $this->assertEquals('SELECT', OnString::removeLeadingBrace("  (  (  SELECT"));
 
     }
+
+    public function testUrlStripFile() {
+        $this->assertEquals('', OnString::urlStripFile(""));
+        $this->assertEquals('www.example.com', OnString::urlStripFile("www.example.com"));
+        $this->assertEquals('www.example.com', OnString::urlStripFile("www.example.com/index.php"));
+        $this->assertEquals('www.example.com', OnString::urlStripFile("www.example.com/index.php?id=100"));
+        $this->assertEquals('www.example.com', OnString::urlStripFile("www.example.com/?id=100"));
+        $this->assertEquals('www.example.com', OnString::urlStripFile("www.example.com/#new"));
+        $this->assertEquals('www.example.com', OnString::urlStripFile("www.example.com/help#new"));
+        $this->assertEquals('www.example.com', OnString::urlStripFile("www.example.com/index.php?help#new"));
+
+        $this->assertEquals('http://www.example.com', OnString::urlStripFile("http://www.example.com/help#new"));
+        $this->assertEquals('http://www.example.com:9090', OnString::urlStripFile("http://www.example.com:9090/help#new"));
+
+        $this->assertEquals('www.example.com/help/sub', OnString::urlStripFile("www.example.com/help/sub"));
+        $this->assertEquals('www.example.com/help/sub', OnString::urlStripFile("www.example.com/help/sub/"));
+        $this->assertEquals('www.example.com/help/sub', OnString::urlStripFile("www.example.com/help/sub/?id=1"));
+        $this->assertEquals('www.example.com/help/sub', OnString::urlStripFile("www.example.com/help/sub/index.php"));
+        $this->assertEquals('www.example.com/help/sub', OnString::urlStripFile("www.example.com/help/sub/index.php?id=1"));
+        $this->assertEquals('www.example.com/help/sub', OnString::urlStripFile("www.example.com/help/sub/index.php?id=1#freak"));
+    }
 }
\ No newline at end of file