Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

2 Answers

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
1  
According to nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite "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
add comment

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

> 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). httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule –  tftd Apr 30 '12 at 15:48
    
@tftd, ough-yeah, for 301 — permanent, sorry. –  poige Apr 30 '12 at 15:57
add comment

Your Answer

 
discard

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.