I have multiple apache rewrite with different Query_String to convert to nginx:

 ServerName oldsite.com 
 RewriteCond %{QUERY_STRING} ^ubb=showflat&Number=662213$ 
 RewriteRule ^/ubbthreads/ubbthreads.php$ http://newsite.com/1/? [R=301,NC,L]
 RewriteCond %{QUERY_STRING} ^ubb=showflat&Number=662214$ 
 RewriteRule ^/ubbthreads/ubbthreads.php$ http://newsite.com/2/? [R=301,NC,L]


server {
    listen       80;
    server_name  oldsite.com;
    location ~ /ubbthreads/{
        if ($arg = "ubb=showflat&Number=662213"{
        rewrite ^ubbthreads/ubbthreads.php http://newsite.com/1/? permanent;
        if ($arg = "ubb=showflat&Number=662214"{
        rewrite ^ubbthreads/ubbthreads.php http://newsite.com/2/? permanent;

Is that the correct way ? Is there another way to do this without if ?


You might be able to use a map for it.

map $arg_Number $new {
    662213 http://newsite.com/1/?
    662214 http://newsite.com/2/?

and then in your location

rewrite ^ $new permanent;

Do note that you can only avoid the if when you redirect every request to that location. If you need to redirect only when Number is something specific or if it's set then you cannot avoid an if and in worse case scenario (only when something specific) you can't use the map at all.

  • Thanks, it worked, note that the trailing "?" has to be in the rewrite and not in the map: rewrite ^ $new? permanent; – Bastien974 Feb 15 '13 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.