Help converting .htaccess rewrite rules to caddyfile?

Howdy. I’m working on installing Kirby CMS on my web server. I’ve got the server itself up and running, and Kirby semi-works, but it needs its rewrite rules to actually do anything.

Here’s the relevant .htaccess section:

<IfModule mod_rewrite.c>

# enable awesome urls. i.e.:
# http://yourdomain.com/about-us/team
RewriteEngine on

# make sure to set the RewriteBase correctly
# if you are running the site in a subfolder.
# Otherwise links or the entire site will break.
#
# If your homepage is http://yourdomain.com/mysite
# Set the RewriteBase to:
#
# RewriteBase /mysite

# block text files in the content folder from being accessed directly
RewriteRule ^content/(.*)\.(txt|md|mdown)$ index.php [L]

# block all files in the site folder from being accessed directly
# except for requests to plugin assets files
#RewriteRule ^assets/plugins/([a-zA-Z0-9\.\-_%=]+)/(.*)$ site/plugins/$1/assets/$2 [L,N]
#RewriteCond $1 !^plugins/[a-zA-Z0-9\.\-_%=]+/assets/.*
RewriteRule ^site/(.*) index.php [L]

# block all files in the kirby folder from being accessed directly
RewriteRule ^kirby/(.*) index.php [L]

# make panel links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^panel/(.*) panel/index.php [L]

# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

</IfModule>

Any assistance here is appreciated. Bonus if I can add additional directories to which it’d allow access for listings, as the CMS isn’t all I use the server for. :slight_smile:

Anyone? I’ve tried doing the following to get the panel rewrite rule working:

	rewrite {
		r ^panel/(.*)
		to {path} {path}/ /panel/index.php
		}

but it just serves up the base index.php file for the site without executing it; my browser displays the URL [hostname]/panel/login.

The following line is in the log file when I try to access /panel:

[IP] - [21/Nov/2016:16:50:36 -0800] "GET /panel/ HTTP/1.1" 302 23

I’m in over my head here. Any assistance would be greatly appreciated.

Would you mind sharing your current Caddyfile? PHP files being delivered as text sounds like you’ve possibly got a mistake in your fastcgi directive.

That rewrite looks good, but could use some additions, I gave it a shot:

# security
rewrite {
    if_op or
    if {file} ends_with .txt
    if {file} ends_with .md
    if {file} ends_with .mdown
    if {uri} starts_with /site/
    if {uri} starts_with /kirby/
    to /index.php
}

# make panel links work
rewrite /panel {
    to /panel/index.php
}

# make site links work
rewrite {
    to {path} {path}/ /index.php
}
1 Like

I’d been screwing around with it earlier, hence the commented parts. It’d either serve up a 404 or PHP depending on what I did. The root index.php seems to load/execute correctly.

[IP] {
	root /var/www/pinopsida.com
	log /var/log/caddy/pinopsida.com.access.log
	errors /var/log/caddy/pinopsida.com.errors.log
	fastcgi / 127.0.0.1:9000 php 
	gzip
	rewrite / /index.php{uri}
#	rewrite {
#		r ^content/(.*)\.(txt|md|mdown)$
#		to index.php
#		}
#	rewrite {
#		r ^site/(.*)
#		to index.php
#		}
	rewrite /panel {
#		r ^panel/(.*)
		to {path} {path}/ /panel/index.php
		}
#	rewrite {
#		r ^(.*)
#		to {path} {path}/ index.php
#		}
}

Honestly, apart from a few small things, that Caddyfile looks pretty good to me.

rewrite / /index.php{uri} worries me. I don’t know for sure if ordering matters, but I’d move it down to the bottom of the caddyfile and change the rewrite to {path} {path}/ /index.php. Nothing appends the URI to the index in that .htaccess file, so I wouldn’t do it here.

Do you get any error logs when Caddy serves up the panel index.php?

1 Like

I replaced my rewrites with yours, and it works almost completely, but now it seems to be throwing 500s on the CSS; one URL in question is http://[IP]/panel/assets/css/form.min.css?v=2.4.0

There’s no output to caddy’s error log, nor to php-fpm’s. :confused:

Ahh, I didn’t know the site would be serving css out of the /panel subfolder. This should solve those 500s (which will be the result of the css request being rewritten to the php file):

# security
rewrite {
    if_op or
    if {file} ends_with .txt
    if {file} ends_with .md
    if {file} ends_with .mdown
    if {uri} starts_with /site/
    if {uri} starts_with /kirby/
    to /index.php
}

# make panel links work
rewrite /panel {
    to {path} {path}/ /panel/index.php
}

# make site links work
rewrite {
    to {path} {path}/ /index.php
}

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