Caddy: Redirect root ("/") only

Created on 12 Feb 2017  路  11Comments  路  Source: caddyserver/caddy

1. What version of Caddy are you running (caddy -version)?

Caddy 0.9.5

2. What are you trying to do?

Redirect from "/" to "/xyz".

3. What is your entire Caddyfile?

localhost

redir / /xyz 302

4. How did you run Caddy (give the full command and describe the execution environment)?

caddy (on Linux, using a binary downloaded from caddyserver.com with locale addon)

5. What did you expect to see?

When requesting http://localhost:2015 I would expect the browser to be redirected to http://localhost:2015/xyz (or ultimately http://localhost:2015/xyz/).

6. What did you see instead (give full error messages and/or log)?

The browser gets caught up in an infinite redirect loop (ERR_TOO_MANY_REDIRECTS in Chrome).

7. How can someone who is starting from scratch reproduce this behavior as minimally as possible?

Create a directory, put the above Caddyfile in it. Create a sub-directory called xyz containing an index.html with any content (hello world). Run Caddy.


As I understand from the docs, the redir directive treats / as a special catch-all value. How would I change this behavior so that it literally just redirects / and nothing else?

Most helpful comment

Ah, I guess for now the correct solution (using a single site) is as follows:

localhost

redir 302 {
  if {path} is /
  / /xyz
}

All 11 comments

I came up with the following workaround Caddyfile:

localhost/xyz {
  root xyz
}

localhost {
  redir / /xyz 302
}

This basically works. Until you create another sub-directory xyz/foo (with an index.html). Then it leads to issue #1327 (if the trailing slash is omitted).

That's a helpful solution @thomasheller. But it seems to me this should be fixed, so that /applies simply to the / route. The catch-all behaviour seems unnecessary - because as far as I'm aware simply ommitting the first parameter makes it a catch-all doesn't it? Aren't the following two currently equivalent?

redir / /xyz 302
redir /xyz 302

Because if so, I think it would be much more obvious is the former related to a literal / instead.

@nottrobin Currently, both directives are equivalent. Internally, redir /xyz is basically an alias for redir / /xyz (see redirect/setup.go).

If would be helpful though to introduce a distinction here as you suggested.

What I intend (omitted in the above examples) is to redirect from / to different sub-directories depending on the user's locale. Using the workaround, I would have to declare each directory as its own site in Caddyfile, duplicating all the directives for each site. If it would be possible to redirect / only, a single site would suffice. In this case, a fix would really simplify things.

Would there be any downside to fixing this -- except that it can break existing Caddyfiles?

Ah, I guess for now the correct solution (using a single site) is as follows:

localhost

redir 302 {
  if {path} is /
  / /xyz
}

Yeah, sorry it's not ideal, it's just that the catch-all redirect seems to be more common (and useful) in general. I'm strongly considering a particular Caddyfile upgrade for version 2.0 (we're nearing 1.0, for reference) that will make it a little easier, and give the Caddyfile better request matching powers, while hopefully keeping it nice and simple. But, someday. :)

@mholt Can this be reconsidered? I've run into this multiple times. Just redir 302 is a catch-all, correct?

Because they're treated the same, there doesn't seem to be a way to have separate redirects for / and catch-all.

I'm changing my domain and URL structure, so I want to 301-redirect some specific popular old pages to their new location in the new domain, I want to 301-redirect the root to the new domain's root, and I want a catch-all 302-redirect to the new domain's root (so I'm able to add more 301 redirects later on in case I forgot about any old important popular/bookmarked URLs). Generally I'm pretty cautious about 301 redirects because I've bitten myself hard before with them.

http://localhost:8080 {
    redir /coolpage http://new.example.com/2018/coolpage
    redir {
        if {path} is /
        / http://new.example.com
    }
    redir / http://new.example.com 302
}

results in:

Activating privacy features... done.
2018/06/03 01:57:17 Caddyfile:7 - Error during parsing: rule with duplicate 'from' value: / -> http://new.example.com

Just a heads up I've achieved the required redirections with the following:

rewrite ^/$ /xyz
redir /xyz https://new.example.com/xyz
redir / https://new.example.com{uri}

but I guess for it to work the redirection should be to a different domain.

FWIW, in Caddy 2, this is done simply by:

redir / https://new.example.com{uri}

since path matching is exact by default.

FWIW, in Caddy 2, this is done simply by:

redir / https://new.example.com{uri}

since path matching is exact by default.

Is it? The following doesn't work for me:

git.example.com {
  redir / https://github.com/newname permanent
  route /oldname/* {
     uri replace /oldname/ /newname/
     redir https://github.com{uri} permanent
  }
}

The goal is to redirect requests to the old project's name gitlab instance to the new project's name github, but send the / url to the new project, not the github homepage. What happens in practice is that / gets redirected to github's homepage.

@nolanl works fine for me:

Caddyfile:

:8881

redir / https://github.com/caddyserver

curl:

$ curl -v localhost:8881
* Rebuilt URL to: localhost:8881/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8881 (#0)
> GET / HTTP/1.1
> Host: localhost:8881
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 302 Found
< Location: https://github.com/caddyserver
< Server: Caddy
< Date: Tue, 08 Sep 2020 05:00:42 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact

This doesn't seem like an issue. Instead of commenting on old closed issues (and possibly triggering emails to be sent to those watching this issue), please ask your usage questions on the Caddy community forums. We prefer to keep the GitHub issue board for bugs and feature requests. Don't forget to fill out the thread template so we can help you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kilpatty picture kilpatty  路  3Comments

lorddaedra picture lorddaedra  路  3Comments

whs picture whs  路  3Comments

PhilmacFLy picture PhilmacFLy  路  3Comments

SteffenDE picture SteffenDE  路  3Comments