41

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
  • 133
  • 4
javipas
  • 1,292
  • 3
  • 23
  • 38

2 Answers2

63

Do you mean something like:

rewrite ^/component(.*)$ $1 last;
womble
  • 95,029
  • 29
  • 173
  • 228
3

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
  • 133
  • 4