Reputation: 13
I'm trying to do the following rewrites:
maindomain.com/country/everythingExceptWork to redirect to country url
location ~ ^/france(?:/(?!work))(?:(.*))$ {
return 301 https://www.maindomain.com/fr/france/;
}
maindomain.com/anycountry/works to redirect to a single url
location ~ ^/(spain|italy|france)/work$ {
return 301 https://www.maindomain.com/en/works/;
}
The issue is that when i test maindomain.com/country/ it works, but if i test it maindomain.com/country (without the last slash) it will use the default rule, this issue started when i changed:
this: location ~ ^/spain(?:/(.*))?$ {
for this: location ~ ^/spain(?:/(?!work))(?:(.*))$ {
NGINX CONF:
server {
listen 80;
server_name maindomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name maindomain.com;
ssl on;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
#reverse proxy
#Default
location / {
return 301 https://www.maindomain.com;
}
#Work ALL COUNTRIES
location ~ ^/(spain|italy|france)/work$ {
return 301 https://www.maindomain.com/en/works/;
}
#France
location ~ ^/france(?:/(?!work))(?:(.*))$ {
return 301 https://www.maindomain.com/fr/france/;
}
#Spain
location ~ ^/spain(?:/(?!work))(?:(.*))$ {
return 301 https://www.maindomain.com/es/spain/;
}
#Italy
location ~ ^/italy(?:/(?!work))(?:(.*))$ {
return 301 https://www.maindomain.com/en/italy/;
}
}
What i'm missing on this change to include country with or without slash ?
this: location ~ ^/spain(?:/(.*))?$ {
for this: location ~ ^/spain(?:/(?!work))(?:(.*))$ {
thanks in advance!
Upvotes: 1
Views: 1276
Reputation: 15478
The last question sign on the location ~ ^/spain(?:/(.*))?$
regex makes the second slash and all the following an optional part. To left the slash optional you need to change your regex from
location ~ ^/spain(?:/(?!work))(?:(.*))$ { ... }
to
location ~ ^/spain(?:/(?!work).*)?$ { ... }
To preserve first capture group, use
location ~ ^/spain(?:/(?!work)(.*))?$ { ... }
Upvotes: 1