Porting Xenforo .htaccess file over to Caddy

1. Caddy version (caddy version):

v2.2.1 h1:Q62GWHMtztnvyRU+KPOpw6fNfeCD3SkwH7SfT1Tgt2c=

2. How I run Caddy:

Caddy run on a server in the cloud behind cloudflare serving xenforo forums software

a. System environment:

Ubuntu Server 20.04

b. Command:

sudo systemctl start caddy

c. Service/unit/compose file:

N/A

d. My complete Caddyfile or JSON config:

{
        experimental_http3
}
www.example.com, example.com {
    file_server
    root * /var/www/example.com/html/
    import /etc/caddy/caddy_security.conf
    log {
        output file /var/log/caddy/example.com.access.log {
                roll_size 3MiB
                roll_keep 5
                roll_keep_for 48h
        }
        format console
    }
    encode gzip zstd
    php_fastcgi unix//run/php/php7.4-fpm.sock
    tls /etc/caddy/ssl/certs/certificate.pem /etc/caddy/ssl/private/key.pem {
        protocols tls1.2 tls1.3
        client_auth {
            mode require_and_verify
            trusted_ca_cert_file /etc/caddy/ssl/certs/origin-pull-ca.pem
        }
    }
}

3. The problem I’m having:

I’m trying to convert a .htaccess file provided by Xenforo that allows for “friendly URL’s”, i.e. doesn’t display the index.php file, but rather the path to the folder containing it.

4. .htaccess file details:

#       Mod_security can interfere with uploading of content such as attachments. If you
#       cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
#       SecFilterEngine Off
#       SecFilterScanPOST Off
#</IfModule>

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 405 default
ErrorDocument 406 default
ErrorDocument 500 default
ErrorDocument 501 default
ErrorDocument 503 default

<IfModule mod_rewrite.c>
        RewriteEngine On

        #       If you are having problems with the rewrite rules, remove the "#" from the
        #       line that begins "RewriteBase" below. You will also have to change the path
        #       of the rewrite to reflect the path to your XenForo installation.
        #RewriteBase /xenforo

        #       This line may be needed to workaround HTTP Basic auth issues when using PHP as a CGI.
        #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
        RewriteRule ^.*$ index.php [NC,L]
</IfModule>

5. Error messages and/or full log output:

No error messages to report, just need some assistance converting the .htaccess file

6. What I already tried:

Tried to port it myself to no avail. I appreciate any assistance anyone can provide here.

7. Links to relevant resources:

N/A

1 Like

Caddy’s php_fastcgi directive comes with try_files logic, so it should work out of the box without an extra rewrite.

What’s typically easier is looking at an nginx config for the app you’re trying to run. Caddy config is closer to nginx than apache. Looking at this example, I think you might not have used the right root, I think you might need to set it to the /xenforo directory.

1 Like

Thank you, @francislavoie

Since this may be useful to future visitors, I’m detailing the steps I took to address this, and to get friendly URL’s working without any additional rewrite directives.

This set me down the right path to fixing this problem I was having. When I had originally installed the forum software, I put it into a folder named “forums”, rather than the root html folder within “/var/www/sitename.com/html/forums/”. To rectify the above, I moved the xenforo installation files to the root of the html folder, changed the root in Caddy to point at “/var/www/sitename.com/html”.

Within Xenforo, under Setup->Options->Basic Options I changed the Board URL to:

https://sitename.com 

Under Index page route: I removed forums/ and blanked out the field

I then checked Use full friendly URLs and now all is working as expected.

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.