![将子目录重定向到外部域](https://linux22.com/image/785925/%E5%B0%86%E5%AD%90%E7%9B%AE%E5%BD%95%E9%87%8D%E5%AE%9A%E5%90%91%E5%88%B0%E5%A4%96%E9%83%A8%E5%9F%9F.png)
我想将某个子目录的所有条目重定向到另一个(外部)域,其中包含地址的“其余部分”。我尝试了一些方法,但没有效果...
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;
}
我究竟做错了什么?
谢谢
答案1
您已将重定向放在 HTTP 块中。请将其放在 https 块中,例如多于它目前位于哪里。
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;
}
}