How can I interrupt the redirect?

1. The problem I’m having:

I’m considering switching from Apache to Caddy.

I am using Apache to perform the following redirect.

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^test.txt$ /test2.txt [R=301,L]

RewriteCond %{REQUEST_URI} (.+[^/])$
RewriteRule ^ %1/ [L,R=301]
</IfModule>

I converted this to Caddyfile, but I don’t know how to interrupt the redirection midway.
In the example below, after being redirected to /test2.txt, it is redirected to /test2.txt/.
In the Apache settings, you can specify that no further redirects will be performed with [R=301,L], but is there a similar method for Caddy?
I know that I can exclude it with not path, but there are quite a lot of paths I want to redirect, so I would like to refrain from using it if possible.
Sorry for the confusing question.

2. Error messages and/or full log output:

curl -LI staging.riku22.net/test.txt

HTTP/1.1 308 Permanent Redirect
Connection: close
Location: https://staging.riku22.net/test.txt
Server: Caddy
Date: Wed, 19 Jun 2024 21:30:48 GMT

HTTP/2 301
alt-svc: h3=":443"; ma=2592000
location: /test2.txt
server: Caddy
date: Wed, 19 Jun 2024 21:30:48 GMT

HTTP/2 301
alt-svc: h3=":443"; ma=2592000
location: /test2.txt/
server: Caddy
date: Wed, 19 Jun 2024 21:30:48 GMT

HTTP/2 404
alt-svc: h3=":443"; ma=2592000
server: Caddy
date: Wed, 19 Jun 2024 21:30:48 GMT

3. Caddy version:

v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

4. How I installed and ran Caddy:

  • Installed from official Cloudsmith ubuntu repository
  • Domain name: staging.riku22.net

a. System environment:

Ubuntu 24.04

b. Command:

systemctl start caddy

c. Service/unit/compose file:

Uses official systemd service.

d. My complete Caddy config:

staging.riku22.net {
	root * /srv/www/staging.riku22.net
	file_server

	redir /test.txt /test2.txt 301

	add_trailing_slash {
		path_regexp (.+[^/])$
	}
	redir @add_trailing_slash {re.1}/ 301
}

Why do you need the “add trailing slash” redirect? Caddy’s file_server already does that automatically for requests to directories (e.g. if /foo is a directory, it’ll redirect to /foo/), so it shouldn’t be needed.

1 Like

Thank you for your reply.
I am eventually rewriting it to a PHP file and am trying to add a trailing slash to it.

I don’t follow. Please elaborate. I don’t see php_fastcgi in your Caddyfile.

1 Like

Sorry.

The complete Caddyfile is below.

staging.riku22.net {
	root * /srv/www/staging.riku22.net
	file_server
	php_fastcgi unix//run/php/php8.3-fpm.sock

	redir /test.txt /test2.txt 301

	add_trailing_slash {
		path_regexp (.+[^/])$
	}
	redir @add_trailing_slash {re.1}/ 301

	@routes path_regexp (.+[/])$
	rewrite @routes /index.php
}

I still don’t think I understand. Please explain the actual usecase. Why do you need the redirect? What problem does it solve?

You should only have trailing slashes on things that look like directories, not on files, because then it breaks relative URLs. Like I said, file_server already takes care of that.

This is very much an https://xyproblem.info/ situation.

1 Like