Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer
According to "context: server, location, if" – poige Apr 30 '12 at 15:30
Yeah.. It appears you're right. Answer was updated. – tftd Apr 30 '12 at 15:44

> redirect — returns a temporary redirect with the code 302; used if a replacement string does not start with “http://” or “https://”;

permanent — returns a permanent redirect with the code 301.

share|improve this answer
I think he should be using permanent in this case because in apache R=301 means Redirect=Code. The code he has posted would generate HTTP status code 301 (Moved permanently). – tftd Apr 30 '12 at 15:48
@tftd, ough-yeah, for 301 — permanent, sorry. – poige Apr 30 '12 at 15:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.