Php – Remove index.php from Nextcloud urls, using Apache+fastcgi

fastcgimod-rewritenextcloudowncloudPHP

I installed Nextcloud on my Webfaction web space, and want to remove the "index.php" in the URL. I followed the instructions here, but then realized that it says:

Furthermore these instructions are only working when using Apache together with the mod_php Apache module for PHP. Other modules like php-fpm or mod_fastcgi are unsupported.

It seems my hoster is using Apache with fastcgi. However, the clean URLs do work – instead of https://example.com/index.php/apps/files/, I can go to https://example.com/apps/files/ and get the correct page. But Nextcloud just goes ahead and inserts the "index.php" when I navigate around. To be precise, a 303 redirect to the long URL is issued.

Also, this forum entry suggests that it does work with php7 and fastcgi.

How can I get it to stop inserting that? I'm fine with editing the code if neccessary, but my experience with PHP is from the time of mysql_* APIs, and before classes, so I'm having a hard time finding my way around 🙂 – maybe somebody here already knows a quick fix for this problem.

Best Answer

Add these lines in Nextcloud's config/config.php:

  'htaccess.RewriteBase' => '/',
  'htaccess.IgnoreFrontController' => true,

Then run occ maintenance:update:htaccess from the console to update the .htaccess file.

You should see these lines added to the end of .htaccess:

<IfModule mod_rewrite.c>
  Options -MultiViews
  RewriteRule ^core/js/oc.js$ index.php [PT,E=PATH_INFO:$1]
  RewriteRule ^core/preview.png$ index.php [PT,E=PATH_INFO:$1]
  RewriteCond %{REQUEST_FILENAME} !\.(css|js|svg|gif|png|html|ttf|woff2?|ico|jpg|jpeg|map|webm|mp4|mp3|ogg|wav)$
  RewriteCond %{REQUEST_FILENAME} !core/img/favicon.ico$
  RewriteCond %{REQUEST_FILENAME} !core/img/manifest.json$
  RewriteCond %{REQUEST_FILENAME} !/remote.php
  RewriteCond %{REQUEST_FILENAME} !/public.php
  RewriteCond %{REQUEST_FILENAME} !/cron.php
  RewriteCond %{REQUEST_FILENAME} !/core/ajax/update.php
  RewriteCond %{REQUEST_FILENAME} !/status.php
  RewriteCond %{REQUEST_FILENAME} !/ocs/v1.php
  RewriteCond %{REQUEST_FILENAME} !/ocs/v2.php
  RewriteCond %{REQUEST_FILENAME} !/robots.txt
  RewriteCond %{REQUEST_FILENAME} !/updater/
  RewriteCond %{REQUEST_FILENAME} !/ocs-provider/
  RewriteCond %{REQUEST_FILENAME} !/ocm-provider/
  RewriteCond %{REQUEST_URI} !^/\.well-known/(acme-challenge|pki-validation)/.*
  RewriteCond %{REQUEST_FILENAME} !/richdocumentscode(_arm64)?/proxy.php$
  RewriteRule . index.php [PT,E=PATH_INFO:$1]
  RewriteBase /
  <IfModule mod_env.c>
    SetEnv front_controller_active true
    <IfModule mod_dir.c>
      DirectorySlash off
    </IfModule>
  </IfModule>
</IfModule>

mod_rewrite and mod_env must be enabled on your server for this to work. You might have to restart the server for the changes to kick in.

This was successfully tested with a Nextcloud 20.0.4 (stable release) on a regular Web install.