Score:0

Redirect a subdirectory to external domain

td flag

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

Score:1
ar flag

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;
    }
}
Maxime Chupin avatar
td flag
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...
HBruijn avatar
in flag
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.
Maxime Chupin avatar
td flag
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 ?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.