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'm trying redirect from an exact folder in nginx.conf

Given the URL: domain.com/path1/path2/path3

Redirect to: sub.domain.com/path1/path2/path3

Here's what I have so far:

location ~* ^/path1[\/?]$ {
    rewrite ^/(.*) http:sub.domain.com/$1 break;
}

I had it working with

location /path1 {
    rewrite ^/(.*) http:sub.domain.com/$1 break;
}

The problem with that is it also redirects a page like domain.com/path1moretext/someotherpath to sub.domain.com/path1moretext/someotherpath

Which is not what I want. (had to take out the "//" in the href code above because this is my first post, sorry).

share|improve this question

2 Answers 2

up vote 0 down vote accepted
location / {

  rewrite ^\/path1\/(.*)$ http://sub.domain.com/$1 last;

  // rest of config for root

}

I'm from an Apache background and I've just started using Nginx in earnest, I've struggled with the re-writes myself, but, I've used the above recently without apparent issue.

share|improve this answer
    
Accomplishes the same thing, thanks. Though I changed the rewrite to: rewrite ^\/path1\/(.*)$ sub.domain.com/path1/$1 break; Not sure of the difference between last and break yet. Something to do with a limit of 10 options when using location? I'm just recalling other documentation from memory here. Thanks! –  user36532 Mar 2 '10 at 21:22
location = /path1 {
  rewrite ^ http://sub.domain.com$uri permanent;
}
location /path1/ {
  rewrite ^ http://sub.domain.com$uri?$args permanent;
}

edit: also read this for information on last/break/permanent/redirect.

share|improve this answer

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.