Php_fastcgi rewrite symfony route

1. The problem I’m having:

Hi,
First of all sorry if my question seems very simple but I can’t get out of it and I haven’t found a corresponding topic.

I have a symphony project and I want to do an internal redirection from a legacy route which no longer exists (/password/forgot) to another route (/api/users/password/reset).
I was thinking of using the rewrite directive (see my caddy config below) but this does not work if I rewrite to another symfony route. However it works for static files like favicon.ico.

I suppose that the rewrite rule is more complicated to write because of the php_fastcgi.

Could you tell me how to do an internal redirection in my situation?

Thanks a lot

2. Error messages and/or full log output:

> POST https://localhost:4430/password/forgot
Status 404
No route found for "POST https://localhost:4430/password/forgot" (404 Not Found)
> POST https://localhost:4430/api/users/password/reset
Status 204

I want to have

> POST https://localhost:4430/password/forgot
Status 204

3. Caddy version:

v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=

4. How I installed and ran Caddy:

a. System environment:

Ubuntu 22.04.3 LTS
Docker version 24.0.5, build 24.0.5-0ubuntu1~22.04.1

c. Compose file:

caddy:
    build:
      context: api/
      target: app_caddy
    depends_on:
      - php
    environment:
      SERVER_API_NAME: ${SERVER_API_NAME:-localhost}, caddy:80
    restart: unless-stopped
    volumes:
      - php_socket:/var/run/php
      - caddy_data:/data
      - caddy_config:/config
    ports:
      # HTTP
      - target: 80
        published: ${HTTP_PORT:-80}
        protocol: tcp
      # HTTPS
      - target: 443
        published: ${HTTPS_PORT:-443}
        protocol: tcp
      # HTTP/3
      - target: 443
        published: ${HTTP3_PORT:-443}
        protocol: udp

d. My complete Caddy config:

{
    # Debug
    {$CADDY_DEBUG}
}

{$SERVER_API_NAME} {
    log

    route {
       
        #rewrite /password/forgot /favicon.ico # This works
        rewrite /password/forgot /api/users/password/reset # This not works

        root * /srv/app/public

        header ?Link `</docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"`
        header ?Permissions-Policy "browsing-topics=()"
        header +Access-Control-Expose-Headers "pagination-current-page, pagination-items-per-page, pagination-total-items"

        php_fastcgi unix//var/run/php/php-fpm.sock
        encode zstd gzip
        file_server
    }
}

The reason the rewrite doesn’t work is because it’s always the original request’s URI which is sent as REQUEST_URI to the PHP app, and that’s what modern PHP apps use for routing. The php_fastcgi directive will rewrite URLs it doesn’t recognize as being file it can serve to index.php – the rewritten URI is used as the PHP script to invoke. You would have to override env REQUEST_URI being passed to PHP, which is pretty hacky, I don’t recommend that.

You might want to consider using a redir instead, especially if this POST is performed by a browser (they’ll follow a redirect for a POST). Make sure to use status code 308 for the redirect so that it preserves the HTTP method correctly.

Are you sure you can’t keep the route in your app itself until it no longer gets hits? You can do an internal re-handling on that route, and include a small bit of logging to keep tabs on whether it’s still getting hits, and after it no longer seems used anywhere you can then remove it.

Thanks a lot for the explanation, I was going crazy.

I will consider using redir but request are also made by an old c++ client. I have to study how it behaves.
If it doesn’t work I will keep the old routes in the app.

Thanks again.

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