2

Hi I want a rewrite rule, but i don't want to redirect it, and requesting one URL but load different content from another URL. So, I think last and break flag in rewrite directive is a good solution. But, it's always redirected, I search the problem and they state that if the new URL not from nginx instance it will redirect instead. Here several lines of my config:

... rewrite ^(/\w+/$)(.+\.php) /$2 last; proxy_pass upstream_name:80; ... 

What i ask is how i can use rewrite but not redirected?

Update: For example, i want to make a new like /link/asd, but there is no /link/asd in my web application. I want my /link/asd link load resource from /asd link. then, i think the rewrite last flag is a good solution. If my understanding is false, please correct me and explain it. Thank you

2
  • 1
    That rewrite statement is not generating the redirect. Commented Oct 13, 2017 at 13:01
  • @RichardSmith yeah it shoud not redirect. But, its actually redirected Commented Oct 14, 2017 at 13:57

1 Answer 1

2

If you want to rewrite exactly /link/example to /example, where example is anything, you can use this rewrite:

rewrite ^/link/(.+)$ /$1 last; 

This will also match /link/example/something/else, as the .+ matches any characters before the end of the string ($). If you want to restrict which characters are allowed in the capture group, for example, exclude / from the capture group, one can use [^/]+ notation for the regex.

2
  • I already use that similar rewrite configuration for instance level and it works. Although, when i use the configuration in reverse proxy level it didn't work. Are you know why? Commented Oct 16, 2017 at 1:34
  • 1
    Please add the configuration file in your question about your attempt. Please include the complete server block. Commented Oct 16, 2017 at 17:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.