Migrating server from subfolder to root folder while keeping subfolder links and sync functional

Nextcloud version (eg, 20.0.5): 23.0.0.10
Operating system and version (eg, Ubuntu 20.04): Ubuntu 20.04 / docker
Apache or nginx version (eg, Apache 2.4.25): 2.4.51
PHP version (eg, 7.4): 8.0.14

We migrated our nc server to a new setup and nc is now running in a docker image with nginx reverse proxy. The setup works fine, just the old nc server was accessible via
https://somedomain.com/nextcloud

and the new server is set up as
https://somedomain.com

Now I want to have all the old links, e.g.
https://somedomain.com/nextcloud/index.php/f/1823725
to be redirected to
https://somedomain.com/index.php/f/1823725
so they continue to work.

Ideally all the users that are using the desktop client for sync do not have to change their configuration, so https://somedomain.com/nextcloud continues to work for the desktop client.

The first one should be possible, however the .htaccess redirect we tried
RewriteRule ^nextcloud/(.*)$ $1
did not work. Every url in nextcloud/ is redirected to the dashboard.

Any ideas if this can work at all (first one should be possible, not sure about the second one) and how we could achieve this?

Thanks and happy new year!

Nobody has any ideas?

Although this question is over a year old, I still want to answer it because if no one has done so yet, it doesn’t mean nobody is interested but rather the solution might be a bit tricky.

I have been running a Nextcloud instance for many years, which I had installed “for test purpose” in a subdirectory and over time, I also wanted to remove the subdirectory from the URL without making the meanwhile thousands of link shares inaccessible. As with @phil11, my first attempts with RewriteRule ^nc/(.*)$ $1 were unsuccessful. Even worse, during the short period when the server was not fully functional on the network, there were various access attempts from customers who had to experience a dead link. So I recreated a Nextcloud instance with the same configuration in a virtual machine and kept trying until I had the solution.

Pre-conditions

  • Full root server access
  • Apache2 (htuser in my case: www-data)
  • Server installation (no docker, no snap)

My Server Setup

/var/www/
       `-- html
       |     `-- index.html (used for redirection to nextcloud:)
       |              <head>
       |                <meta http-equiv='refresh' content='0; URL=https://mydomain.tld/nc/'>
       |              </head>
       `-- nextcloud
             `-- the nextcloud server tree

/etc/apache2/sites-available
               | (with a2ensite linked to sites-enabled):
               `--000-default-le-ssl.conf
               |           <IfModule mod_ssl.c>
               |             <VirtualHost 192.168.189.1:443>
               |               .. (ServerName, ServerAdmin, ErrorLog, CustomLog)
               |
               |               DocumentRoot /var/www/html
               |           # For Client Push (high performance backend for files)
               |           # https://github.com/nextcloud/notify_push
               |               ProxyPass /nc/push/ws ws://127.0.0.1:7867/ws
               |               ProxyPass /nc/push/ http://127.0.0.1:7867/
               |               ProxyPassReverse /nc/push/ http://127.0.0.1:7867/
               |
               |               .. (SSLCertificateFile, SSLCertificateKeyFile etc) 
               |             </VirtualHost>
               |           </IfModule>
               `--000-default.conf
               |           <VirtualHost 192.168.189.1:80>
               |             DocumentRoot /var/www/html
               |             (ServerAdmin, ErrorLog, CustomLog)
               |             RewriteEngine on
               |             RewriteCond %{SERVER_NAME} =(mydomain.tld)
               |             RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
               |           </VirtualHost>
               `--nextcloud.conf
                           Alias /nc "/var/www/nextcloud/"
                           <Directory /var/www/nextcloud/>
                             Require all granted
                             Options FollowSymlinks Multiviews
                             AllowOverride All
                             <IfModule mod_dav.c>
                               Dav off
                             </IfModule>
                           </Directory>

As you can see, Nextcloud is set up to run in the /nc subdirectory and must be accessed through https://mydomain.tld/nc A redirect in the index.html in the DocumentRoot (/var/www/html) forwards all queries (on Port 80 and 443) to http[s]://mydomain.tld to https://mydomain.tld/nc.

So far the pre conditions.

The solution

Short version

    • Create a softlink or mount --rbind the serverinstallation /var/www/nextcloud to /var/www/nextcloud-nc
    • Set DocumentRoot /var/www/nextcloud in <VirtualHost ip:port>-context
    • Comment Alias and set AllowOverride None in nextcloud.conf
    • Move the .htaccess directives into nextcloud.conf file in directory context
    • Copy nextcloud.conf to nextcloud-nc.conf
    • Adapt nextcloud.conf to work without subdir
    • Adapt nextcloud-nc.conf to work with subdir
    • Adapt 'overwrite.cli.url' and 'htaccess.RewriteBase' and add 'check_for_working_htaccess' => false, in config/config.php
    • Check .user.ini
    • Activate

Long version

Step 1

Since I use Pretty URLs and they have to stay the same for the old (subdirectory) Installation and be used for the new (DocumentRoot) installation, i have to create two diferent phisical directories with exact the same content.
There are different ways to do so:
either

  • softlink:
cd /var/www
sudo -u www-data ln -s nextcloud nextcloud-nc

or

  • mount --bind:
mkdir /var/www/nextcloud-nc
chown www-data.www-data /var/www/nextcloud-nc
sudo mount --bind /var/www/nextcloud /var/www/nextcloud-nc

and to make that persistent through reboots, append this line to /etc/fstab:

sudo echo "/var/www/nextcloud /var/www/nextcloud-nc none bind" >> /etc/fstab

No matter what you used, softlink or mount --bind, we have two different directories now with exactly the same content.

Step 2

Change the DocumentRoot and (if used) duplicate the ProxyPass/ProxyPassReverse directives in /etc/apache2/sites-available/000-default-le-ssl.conf like this:

<IfModule mod_ssl.c>
  <VirtualHost 192.168.189.1:443>
     - (ServerName, ServerAdmin, ErrorLog, CustomLog)
# Changed from /var/www/html:
    DocumentRoot /var/www/nextcloud
# For Client Push (high performance backend for files)
# https://github.com/nextcloud/notify_push
# This one is for new version without subfolder:
    ProxyPass /push/ws ws://127.0.0.1:7867/ws
    ProxyPass /push/ http://127.0.0.1:7867/
    ProxyPassReverse /push/ http://127.0.0.1:7867/
# This one is for old version with /nc subfolder:
    ProxyPass /nc/push/ws ws://127.0.0.1:7867/ws
    ProxyPass /nc/push/ http://127.0.0.1:7867/
    ProxyPassReverse /nc/push/ http://127.0.0.1:7867/
    etc ...

Step 3

Now we have to solve the problem with the two different Prety URL RewiteBases /nc and /

Therefor we have to change the /etc/apache2/sites-available/nextcloud.conf:

Comment this line out: Alias /nc "/var/www/nextcloud/"

and change AllowOverride All into AllowOverride None

So it looks like this:

# Alias /nc "/var/www/nextcloud/"
<Directory /var/www/nextcloud/>
  Require all granted
  Options FollowSymlinks Multiviews
# disable .htaccess:
  AllowOverride None
  <IfModule mod_dav.c>
    Dav off
  </IfModule>
</Directory>

Step 4

Coppy the complete content of the /var/www/nextcloud/.htaccess file and paste it into the <Directory /var/www/nextcloud> section, between this:

  </IfModule>

and this

</Directory>

line.

and append this entries at the botom, behind </Directory>:

<Directory /var/www/nextcloud/config/>
  AllowOverride All
</Directory>

<Directory /var/www/nextcloud/data/>
  AllowOverride All
</Directory>

this is to enable the .htaccess files from within those directories to do their work.

It is assumed here that the data directory is in the default place within the Nextcloud tree. If it is not, adjust accordingly.

Step 5

Create a coppy of this file:

sudo cp -a /etc/apache2/sites-available/nextcloud.conf /etc/apache2/sites-available/nextcloud-nc.conf

so now there are two files:

  • nextcloud.conf for the version without subdir
  • nextcloud-nc.conf for the version with /nc subdir

Step 6

Edit nextcloud.conf and change this:

  ErrorDocument 403 /nc/
  ErrorDocument 404 /nc/

into this

  ErrorDocument 403 /
  ErrorDocument 404 /

and this:

    RewriteBase /nc

into this:

    RewriteBase /

Step 7

Edit nextcloud-nc.conf and change the first lines:

# Alias /nc "/var/www/nextcloud/"

<Directory /var/www/nextcloud/>

into this

Alias /nc "/var/www/nextcloud-nc/"

<Directory /var/www/nextcloud-nc/>

and at the end of the file change this (pathes):

<Directory /var/www/nextcloud/config/>
  AllowOverride All
</Directory>

<Directory /var/www/nextcloud/data/>
  AllowOverride All
</Directory>

intos this:

<Directory /var/www/nextcloud-nc/config/>
  AllowOverride All
</Directory>

<Directory /var/www/nextcloud-nc/data/>
  AllowOverride All
</Directory>

and here again is assumed that the data directory is in the default place within the Nextcloud tree. If it is not, you have to remove the <Directory /var/www/nextcloud-nc/data/> section from this file.

Step 8

Edit /var/www/nextcloud/config/config.php and change this:

  'htaccess.RewriteBase' => '/nc',
  'overwrite.cli.url' => 'https://mydomain.tld/nc',

into this:

  'htaccess.RewriteBase' => '/',
  'overwrite.cli.url' => 'https://mydomain.tld',

and add this option:

  'check_for_working_htaccess' => false,

We keep the 'htaccess.RewriteBase' => '/',, to enable nextcloud to create the right .htaccess-files in the future, we only have to maintain them manualy now after each update and look if something has changed and adapt those changes to the server nextcloud[-nc].conf files just created.

Step 9

Check whether values from .user.ini have not yet been transferred to php.ini.


If you fully understand what these steps wil do to your server and you know exactly what you are doing, if you have checked and double checked each step and when you have exact backups from the previous state and you saved all the changes to the respective files then…

It’s time to aktivate everything and make the magic happen:


Step 10

sudo a2ensite nextcloud-nc
sudo systemctl reload apache2

E voilá

For me this workes, if this destroys your installation, you are on your own. I cannot give further support. :stuck_out_tongue_winking_eye:

Proof of concept:
https://global-social.net/nc/s/CLbrSSQdN6iZBGi
https://global-social.net/s/CLbrSSQdN6iZBGi

Even though I have created this post with the greatest possible care, I know with certainty that I (as usual) made at least small mistakes. If you find any inaccuracies please point them out to me, I will correct them immediately if possible or your comment will be the correction.

Happy hacking

1 Like