1

I am using nginx and trying to redirect all users from old domain to new domains.,

using this configuration.

server {
    listen 80;
    server_name example.com;
    return       301 https://new-example.com$request_uri;
}

But while doing this, i want to access directory which is on old domain,

e.g. i want to access example.com/datafiles/

but while using above code every request gets 301 redirected to new domain and url becomes

new-example.com/datafiles/

i dont want urls containing example.com/datafiles/ to gets redirected to new domain.

How can i achieve something like this ?

so far i have tried this.

server {
    listen 80;
    server_name example.com;

    #set location for sitemaps directory
    location /datafiles/ {
        root   /usr/local/nginx/html/example.com/html;
    }

    return       301 https://new-example.com$request_uri;
   }

but still requests to

example.com/datafiles/

gets redirected to

new-example.com/datafiles/

what am i missing here ?

1
server {
    listen 80;
    server_name example.com;

    #set location for sitemaps directory
    location ~ ^/datafiles/ {
        root   /usr/local/nginx/html/example.com/html;
    }
    location / {
    return       301 https://new-example.com$request_uri;
    }
}

This should do the trick.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy