0

I want to redirect all the entries for a certain subdirectory to another (external) domain with the "rest" of the address. I have tried something but it does not work...

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

    server_name cahiers.example.fr cahiers.example.eu.org;

    return 301 https://publications.example.fr/cahiers;
    ssl_certificate /etc/letsencrypt/live/publications.example.fr/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/publications.example.fr/privkey.pem; # managed by Certbot
}


# Bloc pour la redirection vers le HTTPS.
server {
    location ^~ /subdir(.*) {
         return 301 https://external.org/$1;
    }

    if ($host = cahiers.example.fr) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = cahiers.example.eu.org) {
       return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;


    server_name cahiers.example.fr cahiers.example.eu.org;


    return 301 https://$host$request_uri;


}

What am I doing wrong?

Thanks

1 Answer 1

1

You've placed your redirect in the HTTP block. Place it in the block for https, e.g. the one above where it is currently located.

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

    server_name cahiers.example.fr cahiers.example.eu.org;

    return 301 https://publications.example.fr/cahiers;
    ssl_certificate /etc/letsencrypt/live/publications.example.fr/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/publications.example.fr/privkey.pem; # managed by Certbot

    location ^~ /subdir(.*) {
         return 301 https://external.org/$1;
    }
}
3
  • Thank you, but unfortunately that does not work either. I’v tried to put it before the first return 301 to publications.example.fr/cahiers, but that does not work either. Only the return 301 to publications.example.fr/cahiers works... May 12 at 7:48
  • A HTTP 301 redirect is a permanent redirect and will be cached by your browser. - When you make changes in your web server configuration, you need to test each change in a separate new incognito/anonymous browser window. Otherwise the browser will immediately go to the target of the cached redirect and won't actually hit your web server and won't pick up the change.
    – HBruijn
    May 12 at 9:27
  • Thanks for this advice. I’ve tried that in a new incognito/anonymous browser window, but still the same result. Is there a way to test if we «enter» in the location group ? May 12 at 12:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .