chris
chris

Reputation: 2951

Nginx redirect only change first part of URL after domain

I would like to change only the first part of the url on a group of pages.

Eg.

domain.com/change/this/that/other 

to

domain.com/changed/this/that/other 

in the example everything after and including this would be dynamic

I thought this would work

location ~* ^/change/(.*)$ {rewrite ^ http://domain.com/changed/$1 permanent; break;}

but doesnt

Upvotes: 3

Views: 9218

Answers (1)

pythagor
pythagor

Reputation: 316

Regex capture variables relate to rewrite block itself.

location ~* ^/change/(.*)$ {
     rewrite ^/change/(.*)$ http://example.com/changed/$1 permanent; 
     break;
}

You can see the examples: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

Upvotes: 11

Related Questions