Svitlana
Svitlana

Reputation: 2384

nginx conditional redirects rules

I have such a task: http://www.realtyadvisorselite.com/homes-for-sale/skokie to redirect permanently to http://www.realtyadvisorselite.com/residential/homes-for-sale/skokie

another words add "residential" subfolder if it is missing from url I have such server blocks in nginx.conf

        server {
        listen       80;
        server_name  realtyadvisorselite.com;
        return       301     http://www.realtyadvisorselite.com$request_uri;
    }

    server {
    listen 80;
    server_name  www.realtyadvisorselite.com;
    location / {
          proxy_pass http://repar;
    }
}

Sorry, seems like a simple task but I cannot understand nginx regexp approach... Thank you!

Upvotes: 0

Views: 759

Answers (1)

Richard Smith
Richard Smith

Reputation: 49842

You question implies that it is a single URL that requires redirection. So a simple option is to use an exact match location and a return statement:

location = /homes-for-sale/skokie {
    return 301 /residential/homes-for-sale/skokie;
}

If you wanted all URIs that begin with /homes-for-sale to be prefixed with /residential, you could use a prefix location and a return statement:

location ^~ /homes-for-sale/ {
    return 301 /residential$request_uri;
}

See this document for more.

Upvotes: 1

Related Questions