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 just installed and partially configured nginx on a dedicated server of mine. But I've got some trouble understanding the regexp. I would like to make nginx redirect www.mydomain.com/forum/ AND www.mydomain.com/forum (note the missing trailing slash; case-insensitive; same applies for "forums" instead of "forum") to forum.mydomain.com/. This is what I came up with:

location ~* ^/(forum|forums) {
rewrite ^/(.*)/(.*)$ http://forum.mydomain.com/$2? permanent;
}

... but for some reason it works with trailing slashes only. :-/

Please help me! Thanks in advance!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Try:

location ~* ^/(forum|forums)/? {
   rewrite ^/(.*)/(.*)$ http://forum.mydomain.com/$2? permanent;
}

EDIT:

Ok the location seems to be working, the problem is probably the rewrite line.

location ~* ^/forums?/?$ {
   rewrite /forums?/?(.*) http://forum.mydomain.com/$1 permanent;
}

Tested the regex on pcretest and it captured it right, see if it works.

share|improve this answer
    
Thanks for your reply. However this didn't fix the problem :-( –  Oliver A. Mar 10 '11 at 23:37
    
Can you provide example URLS (original and how it should end) –  coredump Mar 10 '11 at 23:42
    
See my initial post: hxxp://www.mydomain.com/forum (please note the missing trailing slash) -> hxxp://forum.mydomain.com/, but still hxxp://www.mydomain.com/forum/foo.bar?a=b -> hxxp://forum.mydomain.com/foo.bar?a=b (replace xx with tt, trying to bypass serverfault.com's limitations here :-P) –  Oliver A. Mar 10 '11 at 23:45
    
Thanks a lot!!! location ~* ^/(forum|forums)?/?$ { rewrite /(.*)?/?(.*) http://forum.mydomain.com/$2 permanent; } works like a charm! –  Oliver A. Mar 11 '11 at 0:02
    
@oliver wait, what? :P –  coredump Mar 11 '11 at 0:04

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.