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 need to setup nginx to rewrite "www.test.com" to "www.new-test.com/test/test?foo=bar&bar=foo". I've tried the following code:

server {
   listen 80;
   server_name test.com www.test.com;

    access_log off;
    autoindex off;

    location / {
        rewrite ^ http://www.new-test.com/dir/test?foo=bar&bar=foo permanent;
    }
}

My problem is: "If you specify a ? at the end of a rewrite then Nginx will drop the original $args". Ok but i need to redirect to the full URL with Queries. How can i do it properly?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You can add $request_uri or $args to any place you want in the URL and also use return with proper http return code 301 for example that is much better to tell the browser you are redirecting it to another place. Your configuration would be like :

server {
    listen 80;
    server_name test.com www.test.com;

    access_log off;
    autoindex off;

    return 301 http://www.new-test.com/dir/test?foo=bar&bar=foo&$args;
    or
    ....
    return 301 http://www.new-test.com$request_uri/dir/test?foo=bar&bar=foo;
}
share|improve this answer
    
+1 for the use of return directive. –  Pothi Oct 2 '13 at 22:51
    
Can you share you config file please, or update your question, also share the url you try to access before getting the redirect and the url you got redirected to. –  montss Oct 3 '13 at 14:10
    
It works! Anybody added the "test.com" in another configuration-file so ... sorry! Thank you so much for your help and the return option :) –  Watze Oct 3 '13 at 14:31

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.