.htacces to Caddyfile

Hello,

I have this (click me) post on stackoverflow regarding Cachet and Caddy. I need to get the .htacces translated into the Caddyfile.

Whenever I visit the website I’m supposed to go through a setup so it redirects me to /setup. However, Caddy thinks setup is a folder but it isn’t. That’s why I think the problem lies in the ‘# Redirect Trailing Slashes If Not A Folder…’ part.

Hopefully someone can translate this into Caddyfile for me, thanks!

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

This part is issuing a permanent redirect based on a regex - unfortunately there’s no way to configure Caddy to do this right now.

You can handle something similar with an internal rewrite, but it won’t be visible to the client.

rewrite {
  if {path} ends_with /
  r ^(.*)/$
  # Try normally first to check directory, fall back to no slash
  to {1}/ {1}
}

The next part is a common convention for Caddy users - the !-d and !-f checks are handled by a very similar structure to the previous example.

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

You could combine the two into a single rewrite block to simplify the Caddyfile.

rewrite { 
  r ^(.*)/?$
  to {1}/ {1} index.php
}

The last part of that file takes the client-provided Authorization header and sets an environmental variable with it. I understand this is common in cases where Apache is configured to use FastCGI instead of a PHP module - in order to make the Authorization header available to the FastCGI process.

Caddy doesn’t have a way to set env vars generally, but you can do it within the context of a FastCGI-proxied request with the env subdirective: https://caddyserver.com/docs/fastcgi

1 Like

I tried the

rewrite {
r ^(.*)/?$
to {1}/ {1} index.php
}

but I get ‘site is currently unable to handle this request.’

Looks like a PHP error.

Mind posting your Caddyfile?

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