diff --git a/core/Command/Maintenance/UpdateHtaccess.php b/core/Command/Maintenance/UpdateHtaccess.php
index 8ff10e800cdcc..84f70acbb7c2a 100644
--- a/core/Command/Maintenance/UpdateHtaccess.php
+++ b/core/Command/Maintenance/UpdateHtaccess.php
@@ -29,7 +29,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln('.htaccess has been updated');
return 0;
} else {
- $output->writeln('Error updating .htaccess file, not enough permissions, not enough free space or "overwrite.cli.url" set to an invalid URL?');
+ $output->writeln('Error updating (or creating) .htaccess file, not enough permissions, not enough free space or "overwrite.cli.url" set to an invalid URL?');
return 1;
}
}
diff --git a/lib/private/Setup.php b/lib/private/Setup.php
index ad1e59135d162..55090a9eff8aa 100644
--- a/lib/private/Setup.php
+++ b/lib/private/Setup.php
@@ -580,6 +580,15 @@ public static function updateHtaccess(): bool {
$setupHelper = Server::get(Setup::class);
+ // Note: The .htaccess file also needs to exist, otherwise `is_writable()`
+ // will return false, even though the file could be written.
+ // This function writes the .htaccess file in that case.
+ if (!file_exists($setupHelper->pathToHtaccess())) {
+ if (!touch($setupHelper->pathToHtaccess())) {
+ return false;
+ }
+ }
+
if (!is_writable($setupHelper->pathToHtaccess())) {
return false;
}