45

I know about the HttpRewriteModule, but I don't really know how to handle regex and I would need to redirect all URLs within a certain directory to another, specifically:

From: example.com/component/tag/whatever

To: example.com/tag/whatever

Could some one tell me how to do this in Nginx?

Teocci
  • 153
  • 4
javipas
  • 1,332
  • 3
  • 23
  • 38

2 Answers2

68

Do you mean something like:

rewrite ^/component(.*)$ $1 last;
womble
  • 96,255
  • 29
  • 175
  • 230
4

Depending where you define the rewrite directive you have two ways to implement it:

A. In the server context

server {
    ...
    rewrite ^/component(.*)$ $1 last;
    ...
}

B. In the location context

location /component {
    rewrite ^/component(.*)$ $1 break;
}

Teo, why did you change the flag last to break? Because, if this directive is put inside of a location context, the last flag will make nginx to run 10 cycles and return the 500 error.

Teocci
  • 153
  • 4