Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 whould like this simple rewrite rule:


to be redirected to:


I looked for some solutions and none actually worked..

I tried:

rewrite ^/somefolder/mypage.aspx?myid=(.*)&tab=overview$  /folder/$1/overview  permanent;


rewrite ^/somefolder/mypage\.aspx\?myid=(.*)&tab=overview$  /folder/$1/overview  permanent;

What am I doing wrong? I'm getting 404

(simpler rules works just fine..)


share|improve this question
up vote 5 down vote accepted

If you want redirect

location ~ /somefolder/mypage.aspx {
    if ($args ~* "^myid=(\d+)&tab=overview") {
        set $mid $1;
        set $args '';
        rewrite ^.*$ /folder/$mid/overview permanent;

Don't forget to configure /folder/$1/overview location.

share|improve this answer
Thanks, the redirect works, but without the paramater ($1)... it is blank – YardenST Mar 16 '13 at 16:05
I updated answer. – Valery Viktorovsky Mar 16 '13 at 16:13
Thank you very much – YardenST Mar 16 '13 at 16:33

A shorter and more correct version of Valery Viktorovsky answer.

location = /somefolder/mypage.aspx {
    if ($arg_tab != overview) { return 404; }
    if ($arg_myid !~ "^\d+$") { return 404; }
    rewrite ^ /folder/$arg_myid/overview? permanent;

Or, hey, it can even be shorter, if you don't need to be verifying the arguments:

rewrite ^/somefolder/mypage.aspx /folder/$arg_myid/$arg_tab? permanent;
share|improve this answer
wish you had posted it earlier :) it would've save me some time – YardenST Mar 16 '13 at 19:15
lol. did you have a lot of rules like that? well, better late than never! also, can i get an accept, then? :) – cnst Mar 16 '13 at 19:30
Valery solution works bottom line :) and there could be only one answer :) – YardenST Mar 16 '13 at 19:34
5… « Make sure that besides working for you, the answer is really good practice. Sometimes after the answer gets accepted, another comes in, uncovering the fact that previous one was in fact a bad hack. » Seriously, there are not just one, but a several different problems with Valery's answer! Every line is essentially wrong, other than the curly braces! – cnst Mar 16 '13 at 20:51

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.