PHP Nginx Redirection – Point few URI to old domain and redirect rest to new Domain

Lately, we had our entire website revamped and as a part of sanity, we redirected a 301 from our old url to new url, which was apparently in a new domain. But here came a challenge, there were a few URI’s we had provided to the the third party and the url they used pointed to the old domain. So we had to write exception rule in our nginx configuration file. After a lot of trial and errors, we figured a working solution for the same. I would like to share the generic version of the configuration file.
We wrote a rewrite rule for the 2 old URIs and redirected the rest of them to new domain successfully.
server {
listen 80 ;
server_name old.domain.com ;
root /var/www/old.domain.com_root;
index index.php index.html index.htm;
#try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
expires off; ## Do not cache dynamic content
add_header Cache-Control "max-age=120, must-revalidate";
gzip on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /olduri-1 {
# rewrite ^/.* https://old.domain.com$request_uri permanent;
try_files $uri $uri/ /index.php?$query_string;
}
location /olduri-2 {
# rewrite ^/.* https://old.domain.com$request_uri permanent;
try_files $uri $uri/ /index.php?$query_string;
}
location / {
return 301 https://new.domain.com$request_uri;
}
}