Create a redirect from `?dir=*` to `/*`

1. The problem I’m having:

Hi,

I want to move from a php based directory lister to using Caddy built-in file_server browse.

The previous urls looks like https://mirrors.tnonline.net/?dir=/haiku/release/r1alpha4.1/sources/p7zip-9.20.1.bep while the new ones are simply https://mirrors.tnonline.net/haiku/release/r1alpha4.1/sources/p7zip-9.20.1.bep

I am struggling to understand if it is possible to copy the dir=foo value and create a http 301 redirect to the correct url.

3. Caddy version:

v2.7.4 => /usr/src/caddy/git/caddy@(devel)

d. My complete Caddy config:

mirrors.tnonline.net:443 {
	tls /etc/letsencrypt/live/mirrors.tnonline.net/fullchain.pem /etc/letsencrypt/live/mirrors.tnonline.net/privkey.pem {
		curves x25519 secp521r1 secp384r1 secp256r1
	}
	log {
		output file /var/log/caddy/mirrors.tnonline.net_443.log {
			roll_size 100MiB
			roll_keep_for 100d
		}
		format json
	}
	encode zstd br gzip
	root * /var/www/domains/mirrors.tnonline.net/
	
	@redirect {
		not file {
			try_files {path} {path}/
		}
	}

	@nobrowse {
		not path /.well-known/
		not path /resources/*
		not path /res/
		not path /index.php
	}

	@cache {
		path /favicon.ico
		path /img/*.*
		path /res/*.*
	}

	@inlineFiles {
		path_regexp ^*\.sh$
	}

	header @inlineFiles {
		content-disposition inline
		content-type text/plain
	}
	route {
		header {
			Strict-Transport-Security "max-age=31968000; preload"
			X-Frame-Options DENY
#			Cache-Control "max-age=1800"
		}
		header @cache {
			Strict-Transport-Security "max-age=31968000; preload"
			X-Frame-Options DENY
			Cache-Control max-age=604800
			# Cache-Control "max-age=31536000"
		}
	}
	@dir {
		query dir=*
	}
	respond @dir 410
#	redir @dir	https://mirrors.tnonline.net/ 301

	file_server @nobrowse {
		browse /www/mirrors.tnonline.net/res/browse.html
		hide /robots.txt /sitemap.txt /index.php /.well-known /resources /favicon.ico /res
		precompressed br zstd gzip
	}
}

5. Links to relevant resources:

What i have tried is

@dir {
    query dir=*
}
redir @dir  https://mirrors.tnonline.net/{query} 301

but it outputs the whole query string including dir= part.

The solution was the following.

redir @dir  https://mirrors.tnonline.net{http.request.uri.query.dir} 301
2 Likes

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