diff --git a/core/Command/Maintenance/UpdateHtaccess.php b/core/Command/Maintenance/UpdateHtaccess.php
index eeff3bf8c62de..625c028d57d77 100644
--- a/core/Command/Maintenance/UpdateHtaccess.php
+++ b/core/Command/Maintenance/UpdateHtaccess.php
@@ -24,7 +24,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 5f91dc1069261..f97e945054556 100644
--- a/lib/private/Setup.php
+++ b/lib/private/Setup.php
@@ -556,6 +556,15 @@ public static function updateHtaccess(): bool {
$setupHelper = Server::get(\OC\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;
}