diff --git a/phpBB/phpbb/textreparser/base.php b/phpBB/phpbb/textreparser/base.php
index 6ac30a0002..84209a4127 100644
--- a/phpBB/phpbb/textreparser/base.php
+++ b/phpBB/phpbb/textreparser/base.php
@@ -201,8 +201,8 @@ abstract class base implements reparser_interface
 	*/
 	protected function guess_magic_url(array $record)
 	{
-		// Look for <!-- m --> or for a URL tag that's not immediately followed by <s>
-		return (strpos($record['text'], '<!-- m -->') !== false || preg_match('(<URL [^>]++>(?!<s>))', $record['text']));
+		// Look for magic URL markers or for a URL tag that's not immediately followed by <s>
+		return preg_match('#<!-- ([lmwe]) -->.*?<!-- \1 -->#', $record['text']) || preg_match('(<URL [^>]++>(?!<s>))', $record['text']);
 	}
 
 	/**
@@ -234,7 +234,10 @@ abstract class base implements reparser_interface
 	*/
 	protected function reparse_record(array $record)
 	{
+		// Guess magic URL state based on actual record content before adding fields
+		$record['enable_magic_url'] = $this->guess_magic_url($record);
 		$record = $this->add_missing_fields($record);
+
 		$flags = ($record['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0;
 		$flags |= ($record['enable_smilies']) ? OPTION_FLAG_SMILIES : 0;
 		$flags |= ($record['enable_magic_url']) ? OPTION_FLAG_LINKS : 0;
diff --git a/tests/text_reparser/plugins/post_text_test.php b/tests/text_reparser/plugins/post_text_test.php
index 8ea71e65f5..ecd06ac9bf 100644
--- a/tests/text_reparser/plugins/post_text_test.php
+++ b/tests/text_reparser/plugins/post_text_test.php
@@ -23,4 +23,67 @@ class phpbb_textreparser_post_text_test extends phpbb_textreparser_test_row_base
 	{
 		return new \phpbb\textreparser\plugins\post_text($this->db, POSTS_TABLE);
 	}
+
+	public function data_reparse_url(): array
+	{
+		return [
+			[ // Reparse the same
+				'<r><URL url="https://www.example.com">https://www.example.com</URL> and some more text test included</r>',
+				'<r><URL url="https://www.example.com">https://www.example.com</URL> and some more text test included</r>',
+			],
+			[ // Reparse without magic URL, shouldn't change
+				'https://www.example.com and some more text test included',
+				'<t>https://www.example.com and some more text test included</t>',
+			],
+			[ // Reparse new format without magic URL, shouldn't change
+				'<t>https://www.example.com and some more text test included</t>',
+				'<t>https://www.example.com and some more text test included</t>',
+			],
+			[ // Reparse with magic URL, should update to text formatter format
+				'Foo is <!-- m --><a class="postlink" href="https://symfony.com/doc/current/service_container.html">https://symfony.com/doc/current/service_container.html</a><!-- m --> good',
+				'<r>Foo is <URL url="https://symfony.com/doc/current/service_container.html">https://symfony.com/doc/current/service_container.html</URL> good</r>',
+			],
+			[ // Reparse new format with magic URL, shouldn't change
+				'<r>Foo is <URL url="https://symfony.com/doc/current/service_container.html">https://symfony.com/doc/current/service_container.html</URL> good</r>',
+				'<r>Foo is <URL url="https://symfony.com/doc/current/service_container.html">https://symfony.com/doc/current/service_container.html</URL> good</r>',
+			]
+		];
+	}
+
+	/**
+	 * @dataProvider data_reparse_url
+	 */
+	public function test_reparse_url(string $input_text, string $expected_text)
+	{
+		foreach ([true, false] as $enable_magic_url)
+		{
+			$record = [
+				'enable_bbcode'			=> true,
+				'enable_smilies'		=> true,
+				'enable_magic_url'		=> $enable_magic_url,
+				'post_text'					=> $input_text,
+				'bbcode_uid'			=> '',
+			];
+
+			$sql = 'INSERT INTO ' . POSTS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $record);
+			$this->db->sql_query($sql);
+
+			$record['id'] = $this->db->sql_last_inserted_id();
+			$record['text'] = $record['post_text'];
+
+			// Call reparse_record via reflection
+			$reparser = $this->get_reparser();
+			$reparser_reflection = new \ReflectionMethod($reparser, 'reparse_record');
+			$reparser_reflection->setAccessible(true);
+			$reparser_reflection->invoke($reparser, $record);
+
+			// Retrieve reparsed post text and compare with expectec
+			$sql = 'SELECT post_id, post_text FROM ' . POSTS_TABLE . ' WHERE post_id = ' . (int) $record['id'];
+			$result = $this->db->sql_query($sql);
+			$actual_text = $this->db->sql_fetchfield('post_text');
+			$this->db->sql_freeresult($result);
+
+			$this->assertSame($expected_text, $actual_text);
+		}
+	}
 }
diff --git a/tests/text_reparser/plugins/test_row_based_plugin.php b/tests/text_reparser/plugins/test_row_based_plugin.php
index 80fe85c8d3..c03ed39096 100644
--- a/tests/text_reparser/plugins/test_row_based_plugin.php
+++ b/tests/text_reparser/plugins/test_row_based_plugin.php
@@ -15,6 +15,7 @@ require_once __DIR__ . '/../../test_framework/phpbb_database_test_case.php';
 
 abstract class phpbb_textreparser_test_row_based_plugin extends phpbb_database_test_case
 {
+	/** @var \phpbb\db\driver\driver_interface */
 	protected $db;
 
 	abstract protected function get_reparser();