May 1, 2012
tom

what is the equivalent rewrite rule in nginx

Question

I was having this rewrite rule in Apache2

RewriteRule country/usa[/]*$            /en/country/united-states [L,R=301,NC]

I have tried to build an equivalent rewrite rule in nginx like this

server {
rewrite ^country/usa[/]*$     /en/country/united-states last;
#the remaining of the virtual host
}

but this didn’t work :( any idea of how to do this?
Thanks for your help

Asked by Alaa Alomari

Answer

Something similar to the code below should do the work:

server {
    ...
    rewrite ^/country/usa$ /en/country/united-states permanent;
}

This will redirect only /country/usa and /country/usa/ with a 301 Moved Permanently status. The other way option is

server {
    ...
    rewrite ^/country/usa([/])?(.*)$ /en/country/united-states/$2 permanent;
}

which will redirect everything after and including /country/usa to the new location.
For example /country/usa/testing will go to /en/country/united-states/testing.

Answered by tftd

Related posts:

  1. Nginx rewrite rule equivalent to Apache version
  2. How to reverse this Nginx rewrite rule?
  3. Hidden nginx rewrite rule (I think)
  4. nginx rewrite rule
  5. nginx rewrite rule to replace .php endings with /

Leave a comment