.Htaccess invalid <If command

Nextcloud version (eg, 20.0.5): 28.0.0 (but problem exists since updating to 27.0.8
Operating system and version (eg, Ubuntu 20.04): Synology DSM 7.1.1
Apache or nginx version (eg, Apache 2.4.25): Apache 2.2
PHP version (eg, 7.4): `php8.0’
The issue you are facing:

Is this the first time you’ve seen this error? (Y/N): no

Steps to replicate it:

  1. get latest .htaccess with
   # Add cache control for static resources
  <FilesMatch "\.(css|js|mjs|svg|gif|png|jpg|ico|wasm|tflite)$">
    <If "%{QUERY_STRING} =~ /(^|&)v=/">
      Header set Cache-Control "max-age=15778463, immutable"
    </If>
    <Else>
      Header set Cache-Control "max-age=15778463"
    </Else>
  </FilesMatch>

The output of your Nextcloud log in Admin > Logging:

PASTE HERE

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

<?php
$CONFIG = array (
  'instanceid' => '',
  'passwordsalt' => '',
  'secret' => '',
  'trusted_domains' => 
  array (
    0 => '',
  ),
  'datadirectory' => '',
  'overwrite.cli.url' => '',
  'dbtype' => 'mysql',
  'version' => '28.0.0.11',
  'dbname' => '',
  'dbhost' => '1',
  'dbtableprefix' => 'oc_',
  'dbuser' => '',
  'dbpassword' => '',
  'logtimezone' => 'UTC',
  'installed' => true,
  'maintenance' => false,
  'theme' => '',
  'loglevel' => 2,
  'trashbin_retention_obligation' => 'auto',
  'mail_smtpmode' => 'smtp',
  'mail_smtpsecure' => 'ssl',
  'mail_from_address' => '',
  'mail_domain' => '',
  'mail_smtpauthtype' => 'LOGIN',
  'mail_smtpauth' => 1,
  'mail_smtphost' => '',
  'mail_smtpport' => '',
  'mail_smtpname' => '',
  'mail_smtppassword' => '',
  'htaccess.RewriteBase' => '/nextcloud',
  'updater.release.channel' => 'stable',
  'debug' => false,
  'mail_sendmailmode' => 'smtp',
  'mysql.utf8mb4' => true,
  'encryption.legacy_format_support' => false,
  'encryption.key_storage_migrated' => false,
  'default_phone_region' => 'DE',
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'filelocking.enabled' => true,
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'redis' => 
  array (
    'host' => 'localhost',
    'port' => 6379,
    'timeout' => 0.0,
    'password' => '',
  ),
);

The output of your Apache/nginx/system log in /var/log/____:

2023-12-17T11:11:46+01:00 [Sun Dec 17 11:11:46 2023] [alert] [client ] /volume1/host/nextcloud/.htaccess: Invalid command '<If', perhaps misspelled or defined by a module not included in the server configuration, referer: /

The issue can be fixed by replacing the Cache control for static recources with

  <FilesMatch "\.(css|js|mjs|svg|gif|png|jpg|ico|wasm|tflite)$">
    Header set Cache-Control "max-age=15778463"
  </FilesMatch>

I believe that i had read this somewhere in the forum here before, but wasn’t able to find it anymore.
Can anyone specifiy why this would not work in my configuration? Since not many people seem to have this issue.

Thank you.

The <If> directive requires the mod_version module, and it seems that this module is not enabled on your Apache server. To achieve the same effect without using <If>, you can stick with the original approach of checking the presence of the “v” parameter using a RewriteCond:

<FilesMatch "\.(css|js|mjs|svg|gif|png|jpg|ico|wasm|tflite)$">
  RewriteCond %{QUERY_STRING} (^|&)v=
  Header set Cache-Control "max-age=15778463, immutable"
  Header set Cache-Control "max-age=15778463" env=!QUERY_STRING
</FilesMatch>

This code uses RewriteCond to check if the query string contains the “v” parameter. If it does, the first Header directive is applied; otherwise, the second one is applied. This should work without relying on the <If> directive.

Make sure to test this configuration after making the change to ensure it behaves as expected.

Much luck,
ernolf

Thank you. This seems to have worked. No 500 Error and cache still works. Let’s see.

1 Like

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.