[Newbie Question] Perform redirect with regex

Hello, (sorry for deleting the template :sweat_smile:)
I have this config in my nginx

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    include conf.d/tls.conf;

    server_name blog.eban.bzh;
    root /var/www/blog/;

    location / {
	return 301 https://ilearned.eu.org;
    }

    location ~ ^/.*/(.*.html)$ {
	return 301 https://ilearned.eu.org/$1;
    }

    location /rss.xml {
        return 301 https://ilearned.eu.org/rss.xml;
    }
}

And I’d like to convert the location things to Caddy. The problem is, I’m a newbie with Caddy so I have no idea how to do this :sweat_smile:

If there is another way to do this without regex, that’s also okay for me :wink: The only thing that i want is to do :

If /*/*.html then:
    redirect 301 https://ilearned.eu.org/$1 (where $1 is the html file name)
Else if /rss.xml then:
    redirect 301 https://ilearned.eu.org/rss.xml
Else:
    redirect 301 https://ilearned.eu.org

What you need is the path_regexp matcher, paired with the rewrite directive.

Best if you try to write it yourself first. You’ll learn how to use Caddy better that way.

If you have trouble, I’ll circle back and help you out some more.

Next time, please don’t delete the template. It’s important so that we understand the full context of your setup. For example, how did you install Caddy (makes a difference for how certain things are configured), what version are you using (if you’re using an outdated version, certain features are not available, or known bugs have already been fixed), etc.

2 Likes

Okay :slight_smile: I’ll try with that, thank you !

Hello, I tried and here is my config

blog.eban.bzh {
    @htmlFiles {
	path_regexp htmlFile ^/.*/(.*.html)$
    }
    rewrite @htmlFiles https://ilearned.eu.org/{re.htmlFile.1}
    rewrite /rss.xml https://ilearned.eu.org/rss.xml
    rewrite * https://ilearned.eu.org

}

It works perfectly for the first rewrite, but it seems that the two other aren’t even checked :thinking:

I’m running Caddy v2.4.3 h1:Y1FaV2N4WO3rBqxSYA8UZsZTQdN+PwcoOcAiZTM8C0I= on Debian 11

Please elaborate. What requests work and which don’t? What happens for the ones that do?

You can enable the debug global option to have Caddy log more information about the rewrites it does.

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