Has anyone mautic running with Caddyserver?

Hello,

does anyone have mautic running on Caddyserver?
I just saw that it looks very promising: GitHub - mautic/mautic: Mautic: Open Source Marketing Automation Software.

And they have a .htaccess file. Would it be possible that Caddy could manage that?

original .htaccess file mautic:

# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
#DirectoryIndex index.php

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Set Authorization header for OAuth1a for when php is running under fcgi
    RewriteCond %{HTTP:Authorization} .+
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Determine the RewriteBase automatically and set it as environment variable.
    # If you are using Apache aliases to do mass virtual hosting or installed the
    # project in a subdirectory, the base path will be prepended to allow proper
    # resolution of the app.php file and to redirect to the correct URI. It will
    # work in environments without path prefix as well, providing a safe, one-size
    # fits all solution. But as you do not need it in this case, you can comment
    # the following 2 lines to eliminate the overhead.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/app.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the start page because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^(?!/(index\.php|index_dev\.php|app|addons|plugins|media|upgrade))(/(.*))$ /index.php$2
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

<IfModule mod_php5.c>
    # @link https://github.com/mautic/mautic/issues/1504
    php_value always_populate_raw_post_data -1
</IfModule>

Caddy can’t make use of a .htaccess file, I’m afraid.

This particular one operates on and from environmental variables, and I don’t believe there’s any method to emulate that in a Caddyfile.

The behaviour that can be translated is attempting to serve a filename, or otherwise redirecting to index.php:

# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/index.php [L]

Assuming you’re not using a URL base, you could write this part in a Caddyfile like so:

rewrite {
  to {path} index.php{uri}
}

Outside of that, it looks like it’s designed to still work without rewrites if required, using a redirect.

RedirectMatch 302 ^(?!/(index\.php|index_dev\.php|app|addons|plugins|media|upgrade))(/(.*))$ /index.php$2

You might get away with something like this in a Caddyfile to achieve a similar effect:

redir 302 {
  if {uri} match ^(?!/(index\.php|index_dev\.php|app|addons|plugins|media|upgrade))(/(.*))$
  / /index.php{uri}
}

Although I’m taking a bit of a stab there and would welcome anyone else’s input for that one.

I have to admit I’m not sure if these will be enough for the app to function as expected.

I am running mautic behind caddy server, but I cheated and mautic runs in apache behind caddy (I just proxy to it).

I had this question of how to do this also.

I thought it might be easier to convert the nginx config but I never got around to posting for help in doing so.

Here is the nginx config mautic suggests:

server { listen 80;
root /home/mautic/public_html;
index index.html index.htm index.php;

error_page 404 /index.php;
    error_page 405 = $uri;

server_name mautic.example.com;

access_log /var/log/access_log main;
error_log  /var/log/error_log;

# redirect index.php to root
rewrite ^/index.php/(.*) /$1  permanent;

#######################################
##  Start Mautic Specific config #####
#######################################

# redirect some entire folders
rewrite ^/(vendor|translations|build)/.* /index.php break;


location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to index.html
    # one option: try_files $uri $uri/ /index.php$is_args$args;
    try_files $uri /index.php$is_args$args;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

# Deny everything else in /app folder except Assets folder in bundles
location ~ /app/bundles/.*/Assets/ {
    allow all;
}
location ~ /app/ { deny all; }

# Deny everything else in /addons or /plugins folder except Assets folder in bundles
location ~ /(addons|plugins)/.*/Assets/ {
    allow all;
}
#location ~ /(addons|plugins)/ { deny all; }

# Deny all php files in themes folder
location ~* ^/themes/(.*)\.php {
    deny all;
}

# Don't log favicon
location = /favicon.ico {
    log_not_found off;
    access_log off;
}

# Don't log robots
location = /robots.txt  {
    access_log off;
    log_not_found off;
}

# Deny yml, twig, markdown, init file access
location ~* /(.*)\.(?:markdown|md|twig|yaml|yml|ht|htaccess|ini)$ {
    deny all;
    access_log off;
    log_not_found off;
}

# Deny all attempts to access hidden files/folders such as .htaccess, .htpasswd, .DS_Store (Mac), etc...
location ~ /\. {
    deny all;
    access_log off;
    log_not_found off;
}

# Deny all grunt, composer files
location ~* (Gruntfile|package|composer)\.(js|json)$ {
    deny all;
    access_log off;
    log_not_found off;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    fastcgi_keep_conn on;
    fastcgi_pass   unix:/var/run/php-fpm/mautic.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

add_header ‘Access-Control-Allow-Origin’ “*”;

}

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